Using the WP_Query Class to Display Content in Your WordPress Site
WordPress makes it possible to pull in specific types of content on your website through the WP_Query(); template class. Place this template tag before The Loop, and it lets you specify which category you want to pull information from.
If you have a category called WordPress, and you want to display the last three posts from that category on your front page, in your blog sidebar, or somewhere else on your site, you can use this template tag.
Looking at some of the parameters
The WP_Query(); template class has several parameters that let you display different types of content such as posts in specific categories, content from specific pages/posts, or dates in your blog archives. Here’s an example of two parameters you can use with the WP_Query(); tag:
posts_per_page=X: This parameter tells WordPress how many posts you want to display. If you want to display only three posts, for example, enter posts_per_page=3.
category_name=X: This parameter tells WordPress that you want to pull posts from the category with this specific slug. If the category slug is books-i-read, for example, enter category_name=books-i-read.
The parameter category_name is slightly misleading because you don’t use the category name, but rather the category slug, which is different.
The WP_Query(); class lets you pass many variables and parameters; it’s not just limited to categories, either. You can use it for pages, posts, tags, and more. Visit the WordPress Codex and read about this feature.
Adding the WP_Query(); tag
Choose which category you want to list posts from and locate the slug that belongs to the category. After you do that, you’re ready to add the WP_Query(); tag to your template. The category slug is usually the same as the category name, except in lowercase with words separated by dashes; for example, a Books category on a website has a books slug. To double-check, visit the Categories page on your Dashboard by choosing Posts→Categories, click the name of the category you want to use, and find the category slug listed. The line of code to display five posts from the Books category looks like this:
<?php $the_query = new WP_Query( ‘posts_per_page=5&category_name=books’ ); ?>
It’s not enough just to add that one line of code, however; you need to use the WP_Query(); class within The Loop in WordPress. Follow these steps to include a WP_Query within The Loop in your template:
On your Dashboard, choose Appearance→Editor.
The Edit Themes page opens.
In the Templates list on the right side of the page, click the template in which you want to display the content.
For example, if you want to display content in a sidebar, choose the Sidebar template: sidebar.php.
The template you select appears in the editor in the middle of the page.
Locate the first closing </ul> or </div> tag in the Sidebar template for the theme you’re using.
If you’re using the Twenty Thirteen theme, for example, the ending </div> looks like this: </div><!– .widget-area →
Type the following code directly above the ending </div> tag:
<?php $the_query = WP_Query( ‘posts_per_page=5&category_name=books’ ); ?> <?php while ($the_query->have_posts()) : $the_query->the_post(); ?> <strong><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></strong> <?php the_excerpt(); endwhile; ?>
In the first line, you can change posts_per_page=5&category_name=books to suit your specific needs. Just change 5 to whatever number of posts you want to appear (there’s no limit!), and change books to the specific category slug that you want to use.
Click the Update File button to save the changes to your template.