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:

Tired of dealing with your slow WordPress website? Email me at brian@pagecrafter.com and mention the code #FreeHosting10 for two free months of lightning-fast WordPress hosting. We will even migrate you for free!

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.