请仔细阅读WP_Query
文档,尤其是part about post types. 表示您需要将要查询的职位类型传递给查询:
$args = array(
\'post_type\' => \'my_custom_post_type\',
\'posts_per_page\' => 1,
);
$query = new WP_Query( $args );
更好的做法是通过挂接
pre_get_posts
. 例如,这也会相应地调整分页。将此放置在
functions.php
:
/**
* Override the main query on a custom post type archive.
*
* @param \\WP_Query $wp_query
*/
function wpse_337567( WP_Query $wp_query ) {
// Only check this on the main query.
if ( $wp_query->is_main_query() && is_post_type_archive( \'my_custom_post_type\' ) && !is_admin() ) {
$query->set( \'posts_per_page\', 1 );
}
}
add_action( \'pre_get_posts\', \'wpse_337567\' );