Count your wordpress search results

The default WordPress search results page is noting but to show you the total results as per your search keywords. You can spice this page up a bit by showing the total search count like
4 Results found or something like this….

Just you need to add some codes and it will look like the image below.

The main code which do this action is:

<?php $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; ?>

Now what you will have to do is as follows.

Step 1: Open search.php page inside your theme folder. Remove the existing search query and put the following code.

<?php $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; ?>
<form name="searchform" method="get" action="<?php bloginfo('home');?>">
<input name="s" type="text" value="<?php echo $key;?>" />
<input name="submit" type="submit" value="Search" />
<h2>Search Results</h2>
<h3><?php echo $count;?> items found on your search words '<?php echo $key;?>'</h3>
<!–Now the search result loop starts–>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<h4><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
$words = explode(" ",strip_tags(get_the_content()));
$content = implode(" ",array_splice($words,0,20));
echo $content; ?>
<?php endwhile; ?>
<?php endif; ?>
<!–Search result loop ends–>
view raw search.php hosted with ❤ by GitHub

Step 2: Save the file and check the updates doing some search.

If you are new to WordPress theme modification then make sure you will not basic WordPress template function like header, sidebar and footer as get_header(); get_sidebar(); and get_footer();

You can modify the code for style purpose or adding more ingredients on search.

Share this article:

4 thoughts on “Count your wordpress search results”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.