3

I'm trying to display the number of posts found by a search.

I found this code $wp_query->found_posts but can't seem to make it work, any suggestions?

<?php

add_action( 'genesis_before_loop', 'genesis_do_search_title' );

function genesis_do_search_title() {

    $title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s %s %s</h1></div>',  $wp_query->found_posts, apply_filters( 'genesis_search_title_text', __( 'results for:', 'genesis' ) ), get_search_query() );

    echo apply_filters( 'genesis_search_title_output', $title ) . "\n";

}

genesis();

Documentation:

https://codex.wordpress.org/Creating_a_Search_Page#Display_Total_Results

http://my.studiopress.com/documentation/snippets/

SilverLink
  • 124
  • 2
  • 15
  • What is in the `found_posts`? If this is a built-in function there should be documentation on it somewhere – Mike Mar 03 '17 at 14:51
  • Okay, that looks good. Have you checked to make sure that `$wp_query->found_posts` has a value? – Mike Mar 03 '17 at 15:12
  • Got it working after reviewing the documentation, needed: global $wp_query; – SilverLink Mar 03 '17 at 15:16
  • That's great. Please post how you got it to work in an answer below so that others can learn from what you were able to do. – Mike Mar 03 '17 at 15:18

4 Answers4

7

global $wp_query; needs to be present. The syntaxe used shoud be:

global $wp_query;
$total_results = $wp_query->found_posts;
0

I cannot really find the documentation for genesis hooks so this might not work, but it might be that your action 'genesis_before_loop' is executed before the loop variables are set. Try using 'loop_start' instead.

<?php

add_action( 'loop_start', 'genesis_do_search_title' );

function genesis_do_search_title() {

    $title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s %s %s</h1></div>',  $wp_query->found_posts, apply_filters( 'genesis_search_title_text', __( 'results for:', 'genesis' ) ), get_search_query() );

    echo apply_filters( 'genesis_search_title_output', $title ) . "\n";

}

genesis();

Let me know if this works!

L L
  • 1,440
  • 11
  • 17
  • Thank you very much for taking the time to leave a reply. Unfortunately that doesn't seem to be the solution. Still displays as " results for: games" instead of "8 results for: games". The problem seems to be way it is printed, but tried echo and variables with the same results. – SilverLink Mar 03 '17 at 14:49
  • Do you maybe know where I can find the documentation for the genesis framework? – L L Mar 03 '17 at 14:50
0

Sorry didn't understand code but if you want to count search result through wp_query, so you can use a counter within the loop and display it outside the loop.

Hope it will help you :)

Suraj Menariya
  • 488
  • 4
  • 13
0

Got it working after further reviewing the documentation and adding:

global $wp_query;

So the code is:

<?php

add_action( 'genesis_before_loop', 'genesis_do_search_title' );

function genesis_do_search_title() {

    global $wp_query;

    $title = sprintf( '<div class="archive-description"><h1 class="archive-title">%s %s %s</h1></div>',  $wp_query->found_posts, apply_filters( 'genesis_search_title_text', __( 'results for:', 'genesis' ) ), get_search_query() );

    echo apply_filters( 'genesis_search_title_output', $title ) . "\n";

}

genesis();

Thank you everyone!

SilverLink
  • 124
  • 2
  • 15