This tutorial answers the frequently asked question, "How do I filter blog posts to include or exclude specific categories on a Genesis Framework powered site?"
WordPress provides a very simple way to do this by using its built-in query_args attribute to apply a filter the WP_Query function, one of the most powerful features of WordPress. WP_Query determines what content displays on a page. Once you know how to use this built-in functionality, youâll want to use it often to fit your specific needs. Using it to filter posts does not require any additional plugins, custom post types or PHP coding. Examples of what you can do by interacting with WP_Query include, but are not limited to:
- Exclude posts from Category X on the homepage
- Change the number of posts displayed per page for a specific post type
- Filter which posts are shown and their display order based on postmeta
- Execute a separate query for different content inside your page
For this tutorial, we are focusing simply on filtering posts displayed on a blog page. It may sound a little complex as you read through it, but the entire process takes less than a minute to complete if you follow the steps in order. It's that simple. The first step is finding the ID number for the category or categories you want to include or exclude on the page display.
Find the Category ID
When you setup a blog page in WordPress, the default behavior is to display all posts regardless of category. Sometimes it's useful to filter the blog page so only specific categories display. To filter for specific categories, you need to know the category ID number. WordPress assigns an internal ID number to every data element that you create that's stored in its data tables.
Navigate to Posts/Categories in the WordPress Admin area.
Notice that the category ID does not show up on this screen. To find the ID, hover your mouse over the category name on the right:
As I hover the mouse pointer over the category name, the URL for that category's page displays in the lower left of my browser window. The query string contains the following text: "tag_ID=3." The number referenced by tag_ID is the ID number of that particular category. Each category will have a different number. To filter your blog posts, jot down the category number(s) that you want to include or exclude. We'll be using them in the next step.
As an alternative, there is a plugin called Reveal IDs that I use on every WordPress install. WordPress versions prior to 2.5 displayed the data element IDs on the admin pages. For some reason, the WordPress developers removed this feature in version 2.5. The Reveal IDs plugin restores the feature by adding the ID column to the admin page display.
Filtering Your Posts
Now that the preliminaries are done, we can get to the main point of this tutorialâfiltering your posts. I'm going to assume that you know how to setup a blog page in WordPress. You create a page and choose the blog template. Right? Open your blog page in the page editor and scroll down to the custom fields meta box:
If you don't see the custom fields meta box on your editing page, scroll up to the top and click screen options. Make sure the Custom Fields checkbox is checked. Close the screen options and scroll back down to the custom fields meta box.
Follow these steps:
- Click the Enter New link
- Enter "query_args" without the quotes into the text field above the link
- Click into the value field and enter "cat=3" without the quotes to include only category 3 posts on the blog page. If excluding the category, insert a minus sign before the category number (cat=-3).
- Click the Add Custom Field button
- Save the page
- View the blog page to test your results
You can also use this same technique to include or exclude more than one category of post. To include multiple categories, use cat=1,2,3 in the values field. Likewise, to exclude more than one category use cat=-1,-2,-3.
I use this technique on my publishing business website to filter posts for books, authors and marketing tips. Each category has its own blog page filtered to display posts only for that category.