Top

How To: Make your WordPress Search Results Unlimited

June 7, 2008 by Tony · Leave a Comment 

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.

[del.icio.us] [Digg] [Facebook] [Google] [MySpace] [Newsvine] [Reddit] [Slashdot] [StumbleUpon] [Technorati] [Windows Live] [Yahoo!] [Email]

Bottom