I have a cpt of Accommodation

Taxonomy of Region

Regions are then added like USA, Canada etc

Then for the posts:

I create a post for a whatever Resort and a child posts of whatever Hotel in that resort ( Hierarchical ) and assign it to a tax term like USA

All good and working 100% perfect

Question: when I go and view say USA

I see all the posts, both parent and child

How can I only show the parent post and not the children on these taxonomy archives?

Figured a simple pre_get_posts would do the trick, but tried like 20 variations with no luck

Suggestions appreciated

1 Answer 1


Figured a simple pre_get_posts would do the trick,

It will, you just need to query posts that have a parent of 0.

Assuming your taxonomy slug/name is literally just region:

function wpse_286405_parents_only( $query ) {
    if ( ! is_admin() && $query->is_tax( 'region' ) ) {
        $query->set( 'post_parent', 0 );
add_action( 'pre_get_posts', 'wpse_286405_parents_only' );
  • Works perfect! I seriously just yelled out right now with excitement. Thank you so much. Most awesome person of the day > Jacob Nov 20, 2017 at 10:24

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.