Skip Navigation

[Resolved] Main Category are showing all parent items and not the ones added to the Main

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

Problem:

The issue here is that the user wanted to exclude the posts that are on the child term of the parent taxonomy being viewed.

Solution:

This can be done with the code below.

add_filter( 'parse_tax_query', 'exclude_child_term_posts' );
function exclude_child_term_posts( $query ) {
    if (!is_admin() && $query->is_main_query() && $query->is_tax( 'categories' )) {
        $query->tax_query->queries[0]['include_children'] = 0;
    }
}

This code can be added to Toolset -> Settings -> Custom Code and esnure that it is activated. Replace 'categories' with the slug of your taxonomy.

This support ticket is created 3 years 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
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 9 replies, has 2 voices.

Last updated by alexD-18 3 years ago.

Assisted by: Shane.

Author
Posts
#1974469
2021-03-04 11_19_50-Categories ‹ Shop Dorset — WordPress – Mozilla Firefox.jpg

This link - hidden link - shows all parent items from the 6 parent categories.

Is it possible for it to only show the ones i added into the category section in the back end.

You can see the (attached) i have only added 8 to the main category, but it shows all of them.

Thanks

#1975009

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

Thank you for getting in touch.
Is it possible for it to only show the ones i added into the category section in the back end.

Can you provide a screenshot of the categories that you want to display vs what is actually displaying now? I'm not quite sure a I understand the issue completely. Currently the link that you've sent appears to be displaying all the posts that are in the Garden pets category.

Thanks,
Shane

#1978173
click.jpg
Fashion & Accessories.jpg

Thanks for the reply.

So I want to do this on all top level Categories.

For example I have one called "Fashion & Accessories" and you can see in the screen shot it only has 4 Posts assigned to it.

But when I click into the (attahced "Click") it then shows all the posts.

But it should only have the 4 that I put in the category "Fashion & Accessories" - hidden link - but currently showing all that are in the sub categories.

thanks

#1978483

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

Ok I believe I understand now. You only want to show the posts that are directly assigned to Fashion & Accessories and not the child terms correct?

Would you mind allowing me to have admin access to the site so that I can have a look. We may need to modify the archive query in order to do this.

I've enabled the private fields for your next reply.

Thanks,
Shane

#1978815

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

I tried to write some custom code to apply to this archive, however it seems that the archive isn't respecting the code that was added. Could you point me to where this archive was created?

Is it this template below?
hidden link

Because when I make any changes to the template it is not reflected on the frontend.

Please let me know.
Thanks,
Shane

#1979453

hidden link - try this one, this is the link we made the archive.

this is the one we built the archive views on

#1979833

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

None of the changes I make are being applied. I will need to do a further investigation on this one to see why my changes are not taking effect.

I should clarify that this is a default behavior of the wordpress archive to include the posts of the child terms, so what we are looking at is a wordpress issue and not a Toolset issue.

Would it be ok if I took a copy of the website where I can disable the plugins and use the default wordpress theme ?

Please let me know and I can go ahead with the additional testing.

Thanks,
Shane

#1979907

Yes, no problem. Take a copy 🙂

#1980209

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Alex,

I was able to work on a solution for this. Add the following to the custom code section of Toolset in Toolset->Settings->Custom Code.

Also please ensure that once you've added it, you've click the activate button.

add_filter( 'parse_tax_query', 'exclude_child_term_posts' );
function exclude_child_term_posts( $query ) {
    if (!is_admin() && $query->is_main_query() && $query->is_tax( 'categories' )) {
        $query->tax_query->queries[0]['include_children'] = 0;
    }
}

This should exclude the child term posts from your archive.

I've tested this on the copy of your site and it works well. Please let me know if you're having any issues adding it to your site.

Thanks,
Shane

#1980793

My issue is resolved now. Thank you!

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