WordPress - modified, dependent and extra Loops
WordPress provides (at least) 3 ways to produce customized post loops - each has its application, sometimes it doesn’t matter which one you use, and sometimes there’s only one way to do it… You’ll need to have a little skill with PHP to follow the examples, but it’s not too complicated, honest…
Background
Every Wordpress url includes an instance of the WP_Query class - based on parameters supplied, dates, categories, tags etc., this provides a set of posts which is then displayed via the Loop.
For example: the index url, the front page, - usually - has the 10, say, most recent posts, ordered by descending date.
An archive url - has the posts of a given month and year
A search url, eg /?q=fish+chips - all the posts that mention fish and chips
(And it’s still a Loop, even if there’s only one post…)
So the flow is: parameters supplied -> WP_Query class -> a list of posts to be looped through.
What if you want a different Loop of posts than that already provided? These are the 3 ways.
1. query_posts(’parameters…’)
For example: query_posts(’category_name=featured’);
will pick out all posts that have the category name (slug…) ‘featured’, and can be used like this (outside all Loops):
query_posts(’category_name=featured’);
// the_loop
if (have_posts()) :
while (have_posts()) : the_post();
the_title();
endwhile;
endif;
You can use it before or after the ‘main’ Loop of the url, it’s completely independent. But there’s another way of using the same function
query_posts($query_string . ‘¶meters…’)
(note the necessary ampersand at the beginning of the string)
Which makes query_posts() into a filter - if a post isn’t in the Loop for that url already, it won’t be appearing…
$query_string is the same query string as appears in the url (if you aren’t using mod_rewrite), so, if you used it on a tag page, for example index.php/?t=fish+chips
query_posts($query_string . ‘&category_name=featured’)
will output a list of all posts tagged with ‘fish’ and ‘chips’ and in the category named ‘featured’
You do have to be a little cautious about conflicts in the supplied parameters here - results may be unpredictable. The full range of possibilities in terms of the different input parameters, and pagination, are a bit beyond topic for this post (… will get to it later). With a little ingenuity, you can use query_posts for nearly everything in terms of custom post lists, but there’s also…
2 get_posts()
$posts = get_posts(’category=4&numberposts=5′);
if( $posts ) {
foreach( $posts as $post ) {
//this is now the Loopsetup_postdata( $post );
// print out the post using the_title(), the_link(), etc
}
}
One of the small vagaries of get_posts is that some template tags, eg. the_content(), aren’t immediately available - this is rectified by the setup_postdata( $post );
You can also use any database column name to output members of the post object:
echo $post->ID
And you can form the query parameters as an array:
$args = array(
‘post_type’ => ‘attachment’,
‘numberposts’ => null,
‘post_status’ => publish,
‘post_parent’ => null,
);
$posts = get_posts( $args );
Again, the full range of parameters can be found here
3 WP_Query()
For completeness - and the modern fashion seems to be to use this slightly often, when the preceding simpler examples will do just fine - you can make another instance of the WP_Query class and do it all from scratch:
$my_query = new WP_Query(’category_name=featured&showposts=5′);
if ($my_query->have_posts()) {
while ($my_query->have_posts()) {
$my_query->the_post();// output the post data
}
}
Note that the query parameters for this function have slightly different naming conventions and default values.
Getting to understand what’s going on when the Loop is created and posts are being output inside it, is the basis of programming WordPress - and the first step in customising your own installation.











Rory
May 4, 2008 @ 6:33 am
Thanks for sharing your knowledge. What’s the simplest method of querying the database to establish if a specific post or Page is published and then use this as a conditional to display certain text or graphic independent of the post and the Loop?
Jesse
May 5, 2008 @ 1:07 am
WordPress conditional tags is the place to start for all that…