I have created a custom post "company" and custom category "company category". I have a question here, I want to hide child category post in primary category.

For Example:

  • Primary Category "First"
    • Sub Category "Second"

I've created two posts "Fname" and "Sname". "Fname" was assign to First category and "Sname" assign to Second Category. I don't want show "Sname" in First category but just "Fname" be display on First category.

Is this possible?

I found a plugin solutions, Just One Category, But all of them are used for normal post type , not custom post type.

I try edit the code like this:

$q_args = array(
    'paged' => $glocal_search_pageds,
    'post_type' => array('company'),
    's' => $s,
    'company_category' => $company_category,
    'posts_per_page' => $companyprp,
    'orderby' => $orderby,
    'order' => $order,
    'tax_query' => array(
              'include_children' => false
  • It is good practice to show us the code you have tried already, working or not, or at least what you have investigated to solve the problem (more than test a plugin).
    – cybmeta
    Nov 27, 2014 at 7:23

1 Answer 1


I think that the best solution is to hook into the pre_get_posts action hook. First, it is checked if we are in an archive of your custom taxonomy, then set include_children to false for the tax_query argument of the query.

add_action( 'pre_get_posts', 'cyb_pre_get_posts' );
function cyb_pre_get_posts( $query ) {
    //Assuming the slug of the custom taxonomy is company-category
    //change it with the correct value if needed
    if( $query->is_tax( 'company-category' ) && $query->is_main_query() && !is_admin() ) {
        $tax_query = array(
                'taxonomy'         => 'company-category',
                'terms'            => $query->get( 'company-category' ),
                'include_children' => false
        $query->set( 'tax_query', $tax_query );

Another approach that seems better (taken from this answer and adapted for custom taxonomy instead of core category taxonomy):

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

For custom queries and secondary loops (see WP_Query):

 $args = array(
             //Rest of you args go here
             'tax_query' => array(
                      'include_children' => false

 $query = new WP_Query( $args );
  • I still thinks that the answer from @ialocin is better for this job as it is filtering the current tax query. :-) Nov 27, 2014 at 7:52
  • Yes, it looks better. I didn't notice your comment. The answer of @ialocin checks foris_category(), changing that for is_tax() should work for this, as this question is for custom taxonomy.
    – cybmeta
    Nov 27, 2014 at 8:02
  • Yes, it does work with is_tax(), actually had the same question a while ago. There is unfortunately not an answer as we solved this in comments. But, yes, I can confirm that it works. +1 Nov 27, 2014 at 8:08
  • yes it's work but when i search category from search mean with ?s=&company_category=firstcategory&cat=firstcategory&search_simple=search it will show all listing again .. have anyway to hide it ?
    – Jack Lee
    Nov 27, 2014 at 8:41
  • That is because the code checks $query->is_tax('company-category'). That means that it is executed only for 'company-category' archives, as that is what you asked. You can add/remove more conditionals as you need. For search archives you could use $query->is_search()
    – cybmeta
    Nov 27, 2014 at 10:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.