WordPress search count results

You want to create a search page in WordPress where you count the results and show them on your page, for example: Searching for “apple” gives 3 results.

For getting this search counting you need to add the following code in your search file where you want to show this “counting sentence”.

First we make these variables:

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

Then we use $key for showing the search query and $count for how much results.

<p>Searching for "<?php echo $key;?>" gives <?php echo $count;?> results.</p>
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s