将自定义帖子类型添加到主查询
注册自定义帖子类型并不意味着它会自动添加到主查询中。你需要使用 pre_get_posts
过滤器将自定义帖子类型添加到主查询。
// Show posts of 'post' and 'book' custom post types on home page
add_action( 'pre_get_posts', 'add_my_post_types_to_query' );
function add_my_post_types_to_query( $query ) {
if ( is_home() && $query->is_main_query() )
$query->set( 'post_type', array( 'post', 'book' ) );
return $query;
}