admin管理员组文章数量:1302939
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?
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?
Share Improve this question asked Mar 10, 2021 at 20:03 scpers0nscpers0n 536 bronze badges1 Answer
Reset to default 0I 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.
本文标签: Query to Exclude Child Pages from Custom Post Type Archive
版权声明:本文标题:Query to Exclude Child Pages from Custom Post Type Archive 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741686763a2392486.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论