Query_posts redux
As I've mentioned before, the query_posts() function can be used to repopulate the values of the query_object. However, as the codex entry for this function is currently empty, I'd thought it may be helpful to list some of the arguments that you can pass to it.
I'll start with the one that I mentioned in the earlier post, category_name. This allows you to retrieve entries in the category named. However, there's also another parameter that you can use for the same task; cat.
There are two differences between cat and category_name. The most immediately obvious difference is that cat works on the category_id rather than the name. You may be thinking at this point, "what's the point then? I might as well use the more readable category_name." However, there is another trick that you can do with cat. When passing in the catgory_id, if you instead stick a - in the front, this will instead have the effect of excluding that category.
For example, if you make the following call:
<?phpquery_posts('cat=-1');?>
This will exclude all the posts that belong only to category 1. There is a proviso however, and it's a fairly big one. Note that I said "that belong only to category 1″. Due to (what I believe is) a flaw in how the query is constructed, if a post belongs to another category as well, it will still be picked up.
You can also restrict the posts by author. If you're running a multi-author blog and want only posts from a single author showing up on the front page, you can use the parameters author_name or author. author_name operates on the user_nicename field, whilst author operates on the author id.
You can also retrieve a single post or page. Why would you want to? Simple. As a consequence of the template hierachy, home.php executes first. This means that you can write a home.php which calls query_post to retrieve a particular page and set that to be your front page. Without any plugins or hacks, you've got a mechanism to run, show and maintain a non-bloggy front page.
To do this, you would use the following parameters. If you want a post to be your front page, you can either use p or name. p is the post-id whilst name would be the post-slug e.g.the following two are equivalent
<?phpquery_posts('p=1'); //using the postid to show the first postquery_posts('name=first-post'); //using the post slug to show the first post?>
More useful perhaps would be to take advantage of WP's new page functionality and use that for you front page. You could perhaps set your about page to be the entry point or maybe your sites colophon. You might even do something a bit more dynamic and set a custom page that shows a list of the latest comments, posts, categories and archives but I digress. To retrieve a particular page, you would use the following:
<?phpquery_posts('page_id=7'); //retrieves page 7 onlyquery_posts('pagename=about'); //retrieves the about page only?>
How about returning all the posts from a particular time period? Well, in that case you can use the parameters hour, minute, second, day, monthnum and year.
These all take a number that represent the appropriate unit of measure. The only one which necessitates a bit of explanation would be day which is asking for the day of the month.
<?phpquery_posts('day=15'); //retrieves all the posts that were posted on the 15th?>
The paged parameter can be used to return all the posts for that given page number e.g. if you've configured WP to display 10 posts and you call paged=2, you would get the posts that would normally be on the second page e.g. posts after the first 10.
And finally, we come to the orderby parameter. This, not surprisingly, allows you to change the ordering of posts. By default, WP returns posts in the usual blog pattern: reverse chronological ordering i.e. latest posts first.
This is the same as calling
<?phpquery_posts('orderby=date');?>
However, what you can also is order by the author or title:
<?phpquery_posts('orderby=author');query_posts('orderby=title'); //returns all the posts in reverse alphabetical order?>
Actually, I'll mention one more parameter: posts_per_page. This one will allow you to set the number of posts you want to retrieve for a given page which means that you make WP only show the latest post on the front page.
What's more, if you want to retrieve every single page (perhaps for a custom archive page or a full article listing), you can set this to -1.
<?phpquery_posts('posts_per_page=-1'); //returns all the posts without pagination?>
Anyways, that's it for today. This post was primarily written for my own benefit but it will hopefully be useful to others. Feel free, of course, to add any suggestions or feedback via the comments form. If you're looking for an example on how to use the function, it may be helpful to look at the first post I made on the subject.
Add to the discussionOlder Entries
query_posts
One of the interesting things of WP 1.5 is the ability to rerun The_loop again in the same template.
Read More