Change Number of Results in Loop: WordPress

Open field of grass

So many results/blades of grass!

If you want to limit or even make UNlimited the number of results produced by the WordPress loop, without starting a new wp_query, I have a solution for you. This will work on an results or archive page, anywhere the loop is used, including results for authors, custom taxonomies, custom post types, or categories.

In this example, we are going to make the number of posts displayed unlimited, though this same method would work to alter the main loop in any way, without discarding the original query. The secret lies in query_posts. Distinct from wp_query, query_posts in meant to alter the existing query while preserving its original parameters. So what we need to do is place this new code right before the loop. The loop typically begins with something like:

So right before that is where we place our new code, which is:

Having posts_per_page set to -1 allows us to display an unlimited number of results. We could also add a ‘&’ and then another parameter after “posts_per_page=-1” to further alter our query. The list of parameters that can be used here is quite exhaustive, but you can find a list here: wp_query parameters.

If you wish to use the more traditional and flexible $args array to specify parameters, that would look more like this:

And that’s all there is to it! It’s nice and simple and to the point, hopefully you like it. You can also alter the loop to query future posts fairly easily, for a little extra functionality if you need it. Let me know if you have any questions about it! You can reach me in the comments here, or email me at brian@brianjohnsondesign.com or tweet @brianjonline. Thanks!

About Brian Johnson

Brian Johnson is a website developer and designer living in Minneapolis, Minnesota with a passion for code and WordPress. He spends his days building WordPress websites for small businesses, developing new code with the online community, and living life.

5 Comments on “Change Number of Results in Loop: WordPress”

  1. Thanks a lot, mate! It really worked!
    But… I have pagination function from the theme which brakes after the second page. The code uses ‘the_posts_pagination’ function.

    Any idea how to fix it?

    Thank you,
    Vasil

    1. Unfortunately there are many things that could go wrong, so without seeing it, I have no idea what your problem might be. You could hire us to take a look, or you may want to reach out to the theme developers as well.

  2. Great. Thank you so much. After a longer period of research finaly I will use this super simple, fancy and short solution for limiting my displayed posts on CTP archive. Like Julie I am very grateful!

  3. THANKS SO MUCH! Was looking for awhile for this solution in order to display all posts on a custom taxonomy archive, and this was EXACTLY what I needed!! Am super grateful!!

  4. Pingback: Display Future Posts Easily with query_posts - WordPress - BJD

Leave a Reply

Your email address will not be published.