I have a post category structure like this:
- news
- news / conferences
- news / newsletter
In Appearance > Menu, I have added 3 menu items.
One each for the categories News, Conferences, Newsletter.
However, the News page also displays posts from Conferences and Newsletter.
How do I exclude posts from subcategories from being displayed in the parent category’s page?
Thanks
I rewrote the code from a post at WP Engineer:
Give it a try by pasting the snippet in your
functions.php
. Please keep in mind that a post can’t belong to both parent category and child category, or you won’t get it displayed.Use
category__in
instead ofcat
in your query.As you know already,
cat
will query a category and it’s sub categories:However, when using
category__in
, you’ll will not get results from the subcategory.The documentation on querying based on categories is here.
If you want to include any and all posts that have the category that is currently being queried, you can use the following. This could potentially include posts that have sub/child categories of the queried category, but only if they also have the parent category. It will not include posts that only have a child category. In other words, given the following:
Category 1
Category 2, which is a child category of category 2
Post A, which has category 1 only
Post B, which has category 2 only
Post C, which has both category 1 and category 2
this query will return posts A and C, but not B
The answer provided by @createscape is a good one, but would exclude posts like post B, which may or may not be what you want. So, depending on what you want to be included in your list of results, you could either use that solution or this one. Additional info here: https://developer.wordpress.org/reference/classes/wp_query/#category-parameters
That works well and prevents posts in child categories from showing up in the parent category listing. However, make sure and have a post in that parent category, or you’ll get a not found error if someone clicks on the parent category menu item.
I wrote my own function in order to exclude subcategory posts from the loop, as I found the code above didn’t work for me.
In my theme archive.php file, above the loop, I list the subcategories:
In my functions.php file, I’ve added the following custom function using pre_get_posts:
Then in the archive.php, below the subcategories, I’ve added the regular WordPress loop which is now being modified by the above function:
Though the WordPress codex says that using “category__in” will exclude posts from subcategories, that didn’t work for me and subcategory posts were still showing.
https://codex.wordpress.org/Class_Reference/WP_Query#Category_Parameters
https://developer.wordpress.org/reference/hooks/pre_get_posts/
Okay, as of 2019 I had to use slightly modified code and it works with any taxonomy. Not only with standard category. I used it with my setup of woocommerce and wp, preventing products from nested categories being displayed in parent cat: