There was a lot of hard coding going on in a number of these answers that I felt like may have disadvantages further down the road. Here is an approach that takes the existing tax_query, and swaps out the “include_children” argument.
As an aside, I did not have any luck setting “include_children” to false in this instance. I only found a zero to work.
function wpse239243_exclude_child_taxonomies( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if (is_tax('product-category')) {
$tax_query = $query->tax_query->queries;
$tax_query[0]['include_children'] = 0;
$query->set( 'tax_query', $tax_query );
}
}
return;
}
add_action( 'pre_get_posts', 'wpse239243_exclude_child_taxonomies', 1 );
Related Posts:
- Can’t get a custom template taxonomy page to display
- Custom Taxonomy Archive Page
- Taxonomy Templates… by hierarchical level?
- How to display a listing template of a certain taxonomy?
- How to use wp_terms_checklist() on front end
- Custom taxonomies, with custom rewrites/slug, AND loading a taxonomy archive template from a plugin
- How can I set up a category “overview” page?
- Which template name to use for taxonomies but not their terms?
- On Taxonomy Template page, want to add Post_Type
- Make Custom Taxonomy Category Use Parent Template
- Custom taxonomy template not working
- Using a different template per Custom Taxonomies for single term archive pages
- Woocommerce custom taxonomies page
- Generic taxonomy-term template page
- 404 for a custom taxonomy?
- Why is my taxonomy template not shown?
- Meta description – template code to get category description
- Create and set templates for custom taxonomies
- Pagination on custom taxonomy
- How to have two different versions of a tag/category/taxonomy archive page?
- How do I create a custom archive page depending on the custom taxonomy type?
- Different single page templates for taxonomies
- Override Taxonomy Template
- Taxonomy Parent Name
- Avoid taxonomy-%term%.php if more than one taxonomy
- Returning One custom taxonomy term name
- Make child taxonomy categories use a set template
- How can I make front page to display custom taxonomy page?
- Why get_page_template() doesn’t show taxonomy template file name?
- pagination not working on custom-taxonomy template
- How to add custom content to custom taxonomy
- how to access the $query variable inside taxonomy-xxx.php template file?
- Template file renders on local install, not on web
- Catch all taxonomy template
- Taxonomy archive uses incorrect template when query strings are used
- How to use custom slug and custom templates for custom taxonomy?
- How to get a terms and posts associated with another term?
- Custom taxonomy archive template not recognised
- Post not showing in my custom advanced search
- How to display custom taxonomy
- WordPress doesn’t respect the template hierarchy?
- How to update WordPress custom SQL Select query for custom taxonomies so that syntax is correct?
- Can you orderby slug in a taxonomy archive page using WP_Query?
- Custom Taxonomy Template Variables Available?
- is_tax not working for custom taxonomy
- Equivalent setup_postdata for Taxonomy Term?
- Using wp_query is it possible to orderby taxonomy?
- “tax_query” parameter not working with WP_Query
- How do I exclude a custom taxonomy from the post loop
- get_posts assigned to a specific custom taxonomy term, and not the term’s children
- How can I get only parent terms?
- How to modify a taxonomy that’s already registered
- Retrieve posts by term id custom query
- Change order of Custom Taxonomy List
- Get the the top-level parent of a custom taxonomy term
- get_the_term_list without links in 3.1
- Inserting terms in an Hierarchical Taxonomy
- get_terms – only top level
- Custom taxonomy terms not showing as list Gutenberg Editor
- Altering the appearance of custom taxonomy inputs
- How to only list the child terms of a taxonomy and not their parents?
- Get term SLUG by term ID
- Is ACF being a honey trap? [closed]
- Get term name from term ID?
- Check if Current Category has Children
- How to use taxonomies on attachments with the new Media Library?
- Hierarchical taxonomy UI
- WordPress taxonomy radio buttons
- How to prevent new terms being added to a custom taxonomy?
- Custom taxonomy, get_the_terms, listing in order of parent > child
- How to remove the tag cloud from custom taxonomy admin page?
- custom taxonomy and pages rewrite slug conflict gives 404
- Inserting a term into a custom taxonomy
- Count posts in custom taxonomy
- How to add images to taxonomies?
- Read-only taxonomy (user can assign term but can’t create or edit existing terms)
- Remove taxonomy base or term from url
- Can you add the visual editor to the description field for custom taxonomies?
- Adding Category/Tag/Taxonomy Support to Images/Media
- Add default WordPress tag meta box to User Profile
- Custom Taxonomy Endpoint Pagination using paginate_links()
- How do I get the top-level terms in a custom taxonomy?
- Check if a post has any term in this custom taxonomy?
- Query Custom Post by Taxonomy Category
- Custom columns on edit-tags.php main page
- Loop through custom taxonomies and display posts
- How do I add a custom taxonomy as an option for menus under “Appearance” > “Menus”
- custom post type taxonomy “tag” archive : no post found
- How to get WooCommerce Product Category Link by ID?
- Order get_terms using a Custom Field
- Add custom taxonomy fields when creating a new taxonomy
- get_term_children for immediate children only (not grandchildren)
- Using pre_get_posts to rewrite search query to display posts from multiple taxonomies
- Custom taxonomies capabilities
- Contact Form 7 – Populate Select List With Taxonomy [closed]
- Query users by custom taxonomy and user role
- Display category posts grouped by taxonomy
- WP REST API no longer supports filter param, so how do I get posts in a custom taxonomy?
- Custom Taxonomy and Tax_Query
- Taxonomy, Terms, and Template Files