Showing the number of search results

If you want to show the number of search results found on the search results template, you can find the value in the variable $wp_query->found_posts. Just add

<?php echo '<p>Found ' . $wp_query->found_posts . ' hits.</p>'; ?>

in your search results template. In case that produces no results, try introducing the global $wp_query variable like this:

<?php global $wp_query; echo '<p>Found ' . $wp_query->found_posts . ' hits.</p>'; ?>

The usual method which involves a new WP_Query object does not work with Relevanssi.

  • Pace McCulloch

    This doesn’t seem to be the # of results, but the number of times that search term was found. Therefore if you search for a term and one page has the term 34 times, the output would be 34 even though there is one result in the list.

  • $query->found_posts is the number of documents with hits. I tried this on my blog, and the number shows the amount of results and didn’t change, even if I added the search term in the documents.

  • Martin

    Hello Mikko,
    if Relevanssi is active, the search result yields less posts than the normal WP search. The difference is high sometimes, eg. without Relevanssi “59 posts found”, with Relevanssi “14 posts found”.

    Posts to be indexed are “all public post types”. There are no other exclusions and restrictions (if I see it correctly).

    Are there any other magic filters? What is the reason for such a difference?

    • Well, it can simply be that Relevanssi is more picky, particularly if you’re using the AND operator.