Display only post types that are direct children of the current taxonomy

I have a hieratical category structure using Taxonomies, with a custom post displayed within them.

Currently the higher level categories display all the posts that are in their child categories. How do I make it only list from the current category only and not their children.

I'm currently using the default code.

?php if (have_posts()) : while (have_posts()) : the_post(); ?

I'm not a programmer and have not been ably to find anything with an exact or close enough match for me to work it out.

Topic custom-post-type-archives custom-taxonomy custom-post-types Wordpress

Category Web


Did you try WP_Query? Using my knowledge and the WP_Query documentation, I did this:

$args = array(
    'post_type' => 'my_post_type', //change the post type here
    'post_status' => 'publish',
    'tax_query' => array(
        array(
            'taxonomy' => 'my_category', //change the taxonomy name here
            'field' => 'id',
            'include_children' => false,
            'terms' => 10 //change the term id here 
        )
    )
);
$_query = new WP_Query($args);

if ($_query->have_posts()):
    while ($_query->have_posts()):
        $_query->the_post();
        
        //do something here the_title() etc
    endwhile;
endif;

wp_reset_query();

The magic should come from the include_children attribute. Test it out :)

Is this what you wanted?

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.