Support » Themes and Templates » How to display the number of search results found

  • At the top of the search page, I would like to display the number of matching results that were found for the particular search.

    Is there a specific command that I can use for this?

    Many thanks for your responses.
    Balraj
    Melbourne, Australia

Viewing 6 replies - 1 through 6 (of 6 total)
  • I’m not sure there’s a way to get at the full queried object for this (as to count # of post/search hits), but through the use of a custom loop you could collect all posts from the search query, count them up, then display that result:

    <?php
    $search_count = 0;

    $search = new WP_Query("s=$s & showposts=-1");
    if($search->have_posts()) : while($search->have_posts()) : $search->the_post();
    $search_count++;
    endwhile; endif;

    echo $search_count;
    ?>

    Orin

    (@orin)

    Wow! This is excellent. Works like a charm.

    A shorter version is just to skip the loop entirely.

    $mySearch =& new WP_Query("s=$s & showposts=-1");
    $NumResults = $mySearch->post_count;

    Very useful; thanks!

    I use this plugin

    Results count 0.1
    By Matthew Taylor.
    Adds info to search results for how many matches were found

    http://www.mtaylor.co.uk/wordpress/resultscount.phps

    but his site is not working for me today 🙁

    After using posts_nav_link(), one can display the number of search results by doing this:

    <?php global $max_num_pages, $paged, $posts_per_page; $wp_query;

    if ($max_num_pages > 1)
    {
    echo ‘Showing results ‘;
    echo $posts_per_page * $paged – $posts_per_page + 1;
    echo ‘-‘;
    echo $posts_per_page * $paged – ($posts_per_page – $wp_query->post_count);
    echo ‘ of ‘;
    echo $max_num_pages * $posts_per_page – ($posts_per_page – $wp_query->post_count);
    }
    ?>

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to display the number of search results found’ is closed to new replies.