A WordPress Developer’s Best Friend

Filed as Features, Guides on May 31, 2007 3:50 pm

Have you ever wondered how some WordPress blogs have all those little sections of content? You know, one list that has the 5 most recent stories in a specific category. Another section has a “aside” story. Yet another list called “news” that has a list of stories, but they never get to the main part of the homepage.

This is sometimes known as a “magazine” or “newspaper” style homepage, and although it is a bit complicated to create, the common denominator is a handy little loop condition called “query_posts“.

I first discovered it, of course, by client request. A client wanted a homepage to have a “sideblog” in one of the sidebars that displayed the title and excerpt from all the posts he put in the “sideblog” category.

In fact, the same strategy is employed here on The Blog Herald. See how in the sidebar, directly under the ads, there is a list of news? Where does that come from? For that matter, where does the feature news on the left side come from?

query_posts — that’s where.

query_posts is a way to qualify the loop. It allows you to tell the look which articles to fetch. And it’s pretty straightforward too. Here are some common uses:

<?php query_posts(‘cat=1′); ?>

Place that little blurb before your loop, and it will only display stories in category 1 (that would be the category ID, not the name).

<?php query_posts(‘showposts=3′); ?>

This will output only 3 stories.

<?php query_posts(‘category_name=Staff Home’); ?>

This one limits the loop to only output stories in the “Staff Home” category (this time, it uses the name, not the category ID).

And the possibilities are nearly endless.

So if you ever wanted to know how the big boys make their themes, there you go. Yes, it’s a bit anticlimactic, but a neat trick nevertheless. Over the next few weeks, I’ll be sharing some other tricks like this to make your WordPress theme super cool using query_posts. For more information on the query_posts qualifier, check out the codex. Lots of cool tips there!

Happy coding!

Tags:

This post was written by

You can visit the for a short bio, more posts, and other information about the author.

Submissions & Subscriptions

Submit the post to Reddit, StumbleUpon, Digg or Del.icio.us.

Did you like it? Then subscribe to our RSS feed!



  1. Wordpress: “query_posts”, la requête à tout faire !June 5, 2007 at 10:00 am
  2. By game machine posted on June 7, 2007 at 9:14 am
    Want an avatar? Get a gravatar! • You can link to this comment

    Very Good!!

  3. Creating an Link Blog Within Your Blog : The Blog HeraldJune 7, 2007 at 11:47 am
  4. Query_Posts and Your Homepage Design: Part 2 : The Blog HeraldJune 29, 2007 at 11:34 am
  5. My new WPMU Site » Blog Archive » Query_Posts and Your Homepage Design: Part 2July 18, 2007 at 6:28 pm
  6. Your title here » Blog Archive » The Journey in Building my WebsiteJuly 19, 2007 at 3:11 pm
  7. iDream » Post Topic » Welcome!August 7, 2007 at 1:41 am
  8. thoughtstart » WordPress and the one and only loopSeptember 13, 2007 at 1:48 am
  9. Some new links for WP Developers « Soon to be the Biggest Blog on the InternetMarch 21, 2008 at 12:10 pm
  10. Weblog Tools Collection » Blog Archive » Define Your Own WordPress Loop Using WP_QueryApril 13, 2008 at 2:00 am
  11. Wp Wordpress » Blog Archive » Define Your Own WordPress Loop Using WP_QueryApril 13, 2008 at 3:01 am
  12. Define Your Own WordPress Loop Using WP_Query | BlogBroker24-7April 13, 2008 at 3:06 am
  13. My New Blog » Blog Archive » Define Your Own WordPress Loop Using WP_QueryApril 17, 2008 at 5:56 am
  14. Define Your Own WordPress Loop Using WP_Query | ajaxtime.comApril 16, 2009 at 2:24 pm
  15. Part 1: Dynamic jQuery Featured Post Slider With Or Without Javascript | New 2 WpFebruary 10, 2010 at 3:08 pm
  16. Speedlinking: 6/6/07October 20, 2010 at 5:42 am