A Primer for WordPress’ query_posts()

I’ve probably looked up code examples for query_posts() more than anything else in the whole Codex. I need to commit a basic example for myself, and in the hope that it will benefit others trying to do this basic, basic thing.

Here goes:

<?php
     $args = array(
                   'cat' => '44',
                   'post_type' => 'post',
                   'posts_per_page' => 15,
                   'paged' => ( get_query_var('paged') ? get_query_var('paged') : 1),
                   );

    query_posts($args);
	
	while (have_posts()) : the_post();
		get_template_part( 'content'); 
	
	endwhile;
	?>

This basic query will go find the last 15 posts in category 11. Just as easy as that. There’s a bunch of other filters you can append to the query or add to the query array: cat=11&year=2009&tag=design to bring back all articles tagged with design in category 11 that were written in the days of yore (2009).

Paste this code into your template files (with surrounding PHP tags) for a quick way to show your posts on a page, on a footer or sidebar, or wherever else you want.