WordPress.org

Ready to get started?Download WordPress

Forums

How to display the number of search results found (7 posts)

  1. balraj
    Member
    Posted 8 years ago #

    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

  2. Kafkaesqui

    Posted 8 years ago #

    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;
    ?>

  3. Orin
    Member
    Posted 8 years ago #

    Wow! This is excellent. Works like a charm.

  4. joelhardi
    Member
    Posted 7 years ago #

    A shorter version is just to skip the loop entirely.

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

  5. Xander
    Member
    Posted 7 years ago #

    Very useful; thanks!

  6. Dgold
    Member
    Posted 7 years ago #

    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 :(

  7. snakefoot
    Member
    Posted 7 years ago #

    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);
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic