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.
Great tutorial – how do I make it so that the blog posts are just a brief sample of each like on this page? I want to display just the headline and first paragraph or so – bonus would be to add the featured image:
http://uacnet.org/10984-2/
Make your adjustment on the Genesis/Theme Settings page, Content Archives section.
this seems to work in the “blog” section of the forum, but I’m looking to create a separate section for posts.
So I’d like my “video” posts to be in the “Resource-Video” page, but I’d like my “how-to” post to be in the “Blog” page, and the “great articles” post to be seen in “Resources-Articles” page. The “query_args” doesn’t show the posts (in “Video” or “Articles”) as they are all appearing under “Blog” page.
Tried the same with page to page and the “post=#” doesn’t work.
Basically, click “resources” and all the child pages should appear on the page, and click the child page “video” and all the ‘video’ posts appear. Hope that makes sense.
Greg,
The query_args feature is for themes built on the Genesis Framework only. It won’t work for other themes. You’re alternative is to use a category link on your menu. This is a standard WordPress feature and works across all themes.
Hi, I tried to add category blog pages to my genesis blog but I can write query_args in the text field above the link. I can not writ in the text field, I can only select an option and query_args is not in the list.
I don´t know how to solve it. I can´t add a screen capture to show you what appear in the list that appear in the text field above the add custom field link but query_args is not in the list…
If you can help me…
Thanks!
I’m not sure what to tell you Andrea, but if you clicked the Enter New link, the drop down should go completely away and be replaced with a text field. If you cannot type anything in that text field, you are most likely experiencing a plugin conflict.
Victor, thanks for your fast answer.
I desactivated all my plugins and I still have the same problem… I can not write what I like in the text field, I only can choose one of the options that appear on the list and query_args is not in the list…
Do you have any idea about how to solve it?
Thanks!
Andrea, please use the contact form on this site and send me your admin login credentials. I’m happy to take a look.
I’m trying the same thing with a Genesis child theme, but don’t get the pagination unless I have that site set as the “blog” page for the entire site. Any idea what I’m doing wrong? I’m using your same technique.
Thank you
With Genesis themes, you never have to set the blog page for the entire site. Make certain you have the pagination you want selected in Genesis Setting Content Archives and you have the number of posts per page set in the Blog Page Template area. If pagination still doesn’t work, check your theme’s functions.php file or page-blog template (if it has one) to make sure the developer didn’t turn the feature off. The last thing, make sure you have enough posts in the filtered category to trigger the pagination. It won’t display unless you have a greater number of posts than the number of posts per page set in the Blog Page Template area.
If none of this works, you need to start troubleshooting for a plugin conflict.
Hi Victor,
How do I include multiple categories using this feature?
For example: Cat ID 6 AND Cat ID 14
Thanks.
Hello Haider,
To include multiple categories, use cat=1,2,3 in the values field.
Victor
Hi Victor,
Thanks for the quick response.
cat=1,2,3 is for an OR relationship – i.e. Cat 1 or Cat 2 or Cat 3
But, I need something for an AND relationship – i.e. Cat 1 and Cat 2 and Cat 3
Any help would be appreciated.
Thanks!
Hi Victor,
No worries, I’ve figured it out:
category__and[]=3&category__and[]=7
Thanks anyway!
Glad you figured it out!
Hello-
I’ve been following YouTube tutorials and reading sites online. I’ve been unable to link any posts to pages or have posts appear under pages and I was wondering if you had any advice as to why/how this query_args would not work for a page. I’ve done everything you’ve said and followed videos describing the same thing yet nothing comes up. All the pages are blank. Do you think this is a problem with CSS for the theme we’ve chosen?
I would be very appreciative of any advice.
Thank you,
Hello Rose,
This tutorial applies to the Genesis Framework. Some theme developers provide other methods for hooking into the wp-query. This is for the Genesis Framework and all Genesis child themes only. Whatever theme you are using should have come with a blog template. Make sure you’ve selected the correct template for the page on which you want to display your posts.
Hm … maybe this is a feature of the Genesis framework? I’ve been trying a lot, even with non-existing cat-id’s, and the result does not change at all.
You might be right!
Hi Victor,
Excellent tutorial, but it does not work on my site. Which WordPress Version do you use?
Best
Adrian
Hi Adrian,
I’m on the current version (4.01) and it works just fine. I’ve been using this on every version since 3.5. I can take a look if you’d like.