This guest post was written by Leland of Theme Lab, where he has released over 50 WordPress themes. In addition to themes, Theme Lab also provides some WordPress guides. If you have WordPress knowledge and are interested in writing a post for Hack WordPress, please contact us.
Sometimes you may not want your search results to be limited by the confines of the standard WordPress Loop. This is a quick code hack to allow a search to return unlimited results, altering the standard WordPress Loop by using a custom query. You can do this in a few different ways. If you have a search template, in search.php you can simple add the following line of code above your Loop.
Find:
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
Add:
<?php $posts=query_posts($query_string . '&posts_per_page=-1'); ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
Make sure you put this code in your search.php only, unless you want unlimited posts on your index or archive pages. The -1 you see can be changed to any positive integer to limit the posts to a number you choose as well.
If you don’t have a search.php in your theme, the next level down in the Template Hierarchy is your Main Index Template, or index.php. You can use a conditional tag for the same effect.
For this we’ll use the same code as above, except wrap it in the is_search() conditional tag, like so:
<?php if (is_search()) { $posts=query_posts($query_string . '&posts_per_page=-1'); } ?>
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
You can use this technique to change the standard Loop limitations of archives, categories, tag pages, and even your main index template - but it would probably be easier to simply change your reading settings for that.



Wednesday, April 23rd, 2008 at 4:44 am
Great post Leland! I think there are plenty of situations where displaying additional search results can be extremely useful.
Tuesday, July 29th, 2008 at 10:05 pm
Thanks for the tip!
I’ll be making this update with the wpSearch plugin — brand new, but looks like it’s going to make my search results a whole lot better too. You should check it out.
Saturday, August 9th, 2008 at 4:09 am
Thank you! Works fine…
Sunday, September 21st, 2008 at 7:06 am
Great! That’s exactly what I needed!
Thanks so much!!!
Byez
Friday, May 22nd, 2009 at 10:08 am
Thanks, works great. Now if only I can get images to show in the search results. Any help would be greatly appreciated.
Wednesday, June 3rd, 2009 at 11:05 pm
you rock dude.
Wednesday, June 17th, 2009 at 11:12 am
Thanks, I’ve been looking for a good way to limit the number of search results. I know that wasn’t your intent with this post, but it helped me out.
Trackbacks/Pingbacks
Leave A Comment
Become one of our
Featured Sites
Recent Trackbacks
Contributing Authors
Archives
Extras
WordPress Hacks Copyright © 2007-2009 | An Apricot Media Website
Template by StudioPress | Custom Design by Kyle Eslick and Blog Design Studio
RSS Feed Email RSS