將自定義帖子型別新增到主查詢
註冊自定義帖子型別並不意味著它會自動新增到主查詢中。你需要使用 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;
}