w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to display posts under the parent taxonomy but not under the child taxonomy

Found out the answer by myself

Here is the new code which displays posts only from the parent category and not the child categories

if (is_tax()) {
    if (get_query_var('securityproducts_categories')) {
        $taxonomy_term_id = $wp_query->queried_object_id;
        $taxonomy = 'securityproducts_categories';
        $unwanted_children = get_term_children($taxonomy_term_id,
$taxonomy);
        $unwanted_post_ids = get_objects_in_term($unwanted_children,
$taxonomy);
        // merge with original query to preserve pagination, etc.
        query_posts( array_merge( array('post__not_in' =>
$unwanted_post_ids), $wp_query->query) );
    }
}
 while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

// Your code

endwhile;




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.