Using WP_Query in WordPress 2.7

So I was trying for the last week or so to use three loops on a single page, to display categorized pages, and couldn’t figure out why it didn’t work. (I used the plugin page category plus to put pages in categories).

Anyways, if you want pages to be shown in a categorical listing, use post_type=page in the query string. Otherwise it’ll only show posts by default, which didn’t happen in previous versions! Mine looked like this: <?php $sportsQuery = new WP_Query(‘category_name=Sports Pages&post_type=page’); ?>

//Loop goes here…

Hope this helps somebody. I know I’ll be coming back to it when I forget!

For more info see: http://codex.wordpress.org/Template_Tags/query_posts


3 Responses to “Using WP_Query in WordPress 2.7”

  • Nathan Youngman Says:

    You can also use get_posts() with an array of parameters, one being ‘post_type’ => ‘page’.

  • Nathan Youngman Says:

    Oh, I should add to that… it returns an array of posts in whatever variable you like. Then you can do something like <?php foreach($latest_articles as $post): ?> By assigning each element to $post, you can still use the_permalink(), the_title(), etc. If you need to use the $post you’re on again in the footer or somewhere, then you just need to copy it to another variable first. Have fun!

  • jeremy.massel Says:

    That’s a good way of doing it too! I just like using the loop. Old habits die hard ;)

Leave a Reply