Hello
we are trying to create a view that would display wordpress posts from a specific category (we're not using custom post types and taxonomies here), excluding from the results any post that is listed inside a subcategory, but not in the parent.
Example, parent category is 'clothes'. Child category is 'pants'
shirt is in clothes
chino is in pants
jeans is in pants
socks is in clothes
I need to see only shirt and socks on the screen.
I can't find a way to achieve it, what do you suggest? tanks
thanks.
it should display all standard WP posts that have a specific top-level category assigned, and no posts with any subcategory assigned.
we will display this view in a post, or a page.
I keep re-reading your description of the categories above and don't understand it.
Let me describe an example to try and clarify what you are aiming for.
Let's say our categories are for locations, and we store countries in the top-level, and regions in the second level.
We might have a post with the categories "USA" and "Florida", another with "USA" and "California", another with "Canada" and "Ontario".
I could set up a View to show posts with the USA top-level category regardless of what state they were in, so it would show both the Florida and California post.
Or is what you are describing that there could be posts with "USA" specified and no-second level region specified, and it is only these posts you want to show? That would be my understanding of "displaying posts from parent category excluding posts from subcategories", so the Florida and California posts would not be included.
My parent category is 'countries'. Child category is 'states'.
USA, Mexico, Canada are in 'countries', and not in 'states'.
Ohio, Chiapas, Ontario are in 'states' and not in 'countries'.
Looping through the category 'countries' I get
Usa, Mexico, Canada, Ohio, Chiapas, Ontario.
Because states is a subcategory of countries.
How can I prevent subcategory results to show up in parent category lists?
Ah. Your original question describes displaying posts, but it seems that you want to display the categories themselves, and only the top-level categories.
You can create a View that queries taxonomy terms (not posts), and insert a Query Filter where you specify that the term should have no parent (because it is a top-level parent), as you can see in the screenshot.
Apologies, we're having a hard time understanding each other.
Again,
My parent **category** is 'countries'. Child **category** is 'states'.
USA, Mexico, Canada are **POSTS** in 'countries', and not in 'states'.
Ohio, Chiapas, Ontario are **POSTS** in 'states' and not in 'countries'.
Looping through the category 'countries' I get **POSTS**:
Usa, Mexico, Canada, Ohio, Chiapas, Ontario.
I want to see only **POSTS** Usa, Mexico, Canada on the screen, and **NOT** Ohio, Chiapas, Ontario.
Yes, sorry, we really are talking at cross purposes, and I still don't understand your last comment because it looks like you are using posts and categories interchangeably, rather than categories as something that you assign to posts.
I don't know what you mean by "Looping through the category 'countries'" unless you are talking about a taxonomy query, but that returns taxonomy terms, e.g. categories, and not posts.
However, based upon what I think you are describing, you will need two Views.
The first View is as I described above: it is a taxonomy View which queries Categories in the Content Selection (see screenshot).
Include the filter to only return top-level categories.
In the Loop Output section you will need to insert a second View.
That View will be a posts View, that will display standard posts (your categories are assigned to standard posts, yes?).
In that View add a Query Filter to show posts with a category set by the parent taxonomy View (see screenshot).
In the output section include the post fields you require.
When you add this second View to the output section of your first View, inserting the first View on a page will show whatever posts are assigned to top-level categories.
If that is not what you need then let me know and I'll assign this to someone else to see if they can help.
Because by default it not only shows posts stored in the COUNTRIES category, but also all the posts stored in all child categories of COUNTRIES. So ohio chiapas and ontario show up even if they have no flag in the COUNTRIES category checkbox, indeed they have a flag in the STATES categories checkbox. I want to avoid those results in my list and I thought wiews may help reaching this goal.
OK, the screenshots were very helpful in clarifying what you want, thank you.
You want to exclude child terms from the category archives.
Since WP 4.4 it has included child terms by default, and to exclude them you need to modify the archive query.
Toolset doesn't haven't any provision for this. You can customise the category archive, but the isn't a setting for the has_children argument.
So you'll need to add some custom code to modify the category archive to your site.
You can find code to do exactly this here: hidden link
Simply copy and paste the code in that link into your theme's functions.php file (or using a plugin such as Code Snippets).
That code works for the standard WP categories. If you wanted to do the same with a custom taxonomy the code would need modifying. Let me know if that's the case.