Stop all sub-category posts from showing in it's parent category/s

hello,

I would like to prevent my post tagged in a sub-category from showing in a parent category.

Is there a way of doing this?

eg.
Cars

  • mercedes

I want the post tagged in Mercedes to not be visible in Cars category as well.

Thanks,

Jack

Hi Jack,

This is possible with custom code but this is outside the scope of support that we offer.

To give you something to start with, you can try adding the code below in your child theme’s functions.php file

function wpse_filter_child_cats( $query ) {

if ( $query->is_category ) {
    $queried_object = get_queried_object();
    $child_cats = (array) get_term_children( $queried_object->term_id, 'category' );

    if ( ! $query->is_admin )
        //exclude the posts in child categories
        $query->set( 'category__not_in', array_merge( $child_cats ) );
    }

    return $query;
}
add_filter( 'pre_get_posts', 'wpse_filter_child_cats' );

Hope that helps

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.