I have a taxonomy shared between and Parent and Child post types. When I display the taxonomy archive for a single term it returns the child posts as well as the main posts. I want to limit the archive display to parent posts only.
I referred to this post: https://toolset.com/forums/topic/remove-cpt-from-categorycategory-slug-page-2/
which seems like exactly what I want to do but since the output is in a loop I can't quite figure out where to put the conditional.
If I wrap the Loop Item in the conditional I get gaps in the result display.
Dear EdG4627,
I suggest you simply try with "pre_get_posts" hook, like this:
Add below codes into your theme/funcitons.php:
function sbt_exclude_category($query){
if ( is_tax( 'my-taxonomy-slug' ) && $query->is_main_query() && ! is_admin() ) {
$query->set('post_type',array('paret-cpt-slug'));
}
}
add_action('pre_get_posts','sbt_exclude_category');
Please replace "my-taxonomy-slug" with your custom taxonomy slug, replace "paret-cpt-slug" with the parent post type slug.
More help:
https://developer.wordpress.org/reference/hooks/pre_get_posts/