Skip to content

Instantly share code, notes, and snippets.

@sanjaybhowmick
Created September 10, 2015 03:10
Show Gist options
  • Save sanjaybhowmick/de02e2aead12fecd793d to your computer and use it in GitHub Desktop.
Save sanjaybhowmick/de02e2aead12fecd793d to your computer and use it in GitHub Desktop.
Count your wordpress search results
<div>
<?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" />
</form>
<div></div>
</div>
<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(); ?>
<div>
<h4><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h4>
<p>
<?php
$words = explode(" ",strip_tags(get_the_content()));
$content = implode(" ",array_splice($words,0,20));
echo $content; ?>
</p>
</div>
<?php endwhile; ?>
<?php endif; ?>
<!--Search result loop ends-->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment