Blog

Your Vision, Our Passion

Loop with Query Posts

I was running into a problem where I needed to overwrite the default number of posts that get display. I was working on a taxonomy archive page and it was showing at the most 10 posts.

I knew the amount of post that it was showing was because in the WordPress Dashboard there is a setting Where you can set the number of post that can be shown on any given page. This is set under Settings > Reading.

I wanted to use the default loop on my taxonomy archive page and just overwrite a couple parameters.

I ran into a great article on Digwp.com that showed different ways of using the WordPress Loop. You can head over and read the Article 4 Ways to Loop with WordPress.

Loop with query_posts();

<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=3&cat=-6,-9&order=ASC'); ?>

<?php // DEFAULT LOOP GOES HERE ?>

<?php wp_reset_query(); // reset the query ?>

This code above uses the default query and sets the posts shown to 3 excludes category 6 and 9 and shows them in Ascending order.


In my case I just needed to overwrite the default setting which was 10. I needed to show all posts in my taxonomy archive page. I just added the following before the loop.

<?php global $query_string; // required
$posts = query_posts($query_string.'&posts_per_page=-1&order=ASC'); ?>

<?php // DEFAULT LOOP GOES HERE ?>

<?php wp_reset_query(); // reset the query ?>

This code above uses the default query and sets the posts shown to -1 (all) and shows them in Ascending order.