Skip Navigation

[Resolved] How to exclude child terms from category archives

This thread is resolved. Here is a description of the problem and solution.

Problem:
How to exclude child terms from category archives

Solution:
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: https://gist.github.com/CodeProKid/1d21fb7635141491c7c1facd5e99d1ad

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.

This support ticket is created 5 years, 11 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+01:00)

Tagged: 

This topic contains 9 replies, has 2 voices.

Last updated by Nigel 5 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#907226

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

#907358

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Can I check, where do you intend to display this list?

And it should display all standard WP posts that have any top-level category assigned, is that right?

#907390

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.

#907445

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Sorry.

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.

#907454

thanks let me be more specific.

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?

#907456

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2018-06-01 at 14.13.32.png

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.

#907458

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.

#907484

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

Screen Shot 2018-06-01 at 15.43.39.png
Screen Shot 2018-06-01 at 15.40.09.png

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.

#907511
Screen Shot 2018-06-01 at 17.24.48.png
Screen Shot 2018-06-01 at 17.22.38.png
Screen Shot 2018-06-01 at 17.22.23.png
Screen Shot 2018-06-01 at 17.22.08.png

Well I really don't know how how I would be using posts and categories interchangeably when I am stating exactly what is what. Please see screenshots.

CATEGORIES: CAPITAL LETTERS
posts: lowercase

- ohio (is a post in category:) STATES
- chiapas (is a post in category:) STATES
- ontario (is a post in category:) STATES

- usa (is a post in category:) COUNTRIES
- mexico (is a post in category:) COUNTRIES
- canada (is a post in category:) COUNTRIES

STATES is a child category of parent category COUNTRIES:
COUNTRIES
- STATES

wordpress default archive for category COUNTRIES lists:

- ohio
- chiapas
- ontario
- usa
- mexico
- canada

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.

#907678

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+01:00)

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.