I wanted to a page which displayed posts in a specific category, on a given date and didn't want to write some custom SQL query for it.
Luckily, the WordPress API makes it easy to modify the query even while in the template.
A global variable called $wp_query is available during most parts of the code.
This snippet below lets you list:
- All posts (no paging)
- In chronological order
- and listed in the 'Links' category
$wp_query->set('nopaging', true);
$wp_query->set('order', 'ASC');
$wp_query->set('category_name', 'Links');
$wp_query->query($wp_query->query_vars);
get_template_part('loop');
Now you've got a snippet that does just that, in a few lines of code without having to write up any SQL at all!
The "loop" and argument in get_template_part() can be any template name you want.
You can also filter by tags and categories (or exclude), limit the number of posts shown, restrict by date range, author or paging options.
See the documentation for many more options.
[ Documentation ]