0

I have a hierarchical custom post type called "Services". On the Services archive page I want to display only parent Services and exclude child Services.

I found similar answers to my question, which is to customize the main query within archive-services.php using the following:

function exclude_children( $query ) {
    if ( $query->is_main_query() && !is_admin() && $query->is_post_type_archive( 'services' ) ) {
        $query->set( 'post_parent', 0 );
    }
}
add_action( 'pre_get_posts', 'exclude_children' );

But the child services are still displayed. Why doesn't this work?

1 Answer 1

0

I realized the reason it doesn't work: this function needs to be located in functions.php.

Per WP code reference: pre_get_posts "Fires after the query variable object is created, but before the actual query is run." Since I placed the function within an archive template, the main query had already fired, and was thus too late.

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.