如果需要:
查询页面
每页保留12篇文章,而不是将所需的文章“粘贴”在所需的12篇文章之上
只需在首页显示这些帖子
您可以尝试以下方法
$ids_args = [
\'post_type\' => \'products\'
\'posts_per_page\' => -1,
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'_price\',
\'order\' => \'ASC\',
\'fields\' => \'ids\'
];
$all_posts_ids = get_posts( $ids_args );
// Make sure we have posts before continuing
if ( $all_posts_ids ) {
// Set all our posts that should move to the front
$move_to_front = [12,13,14,34];
// Add the array of posts to the front of our $all_posts_ids array
$post_ids_merged = array_merge( $move_to_front, $all_posts_ids );
// Make sure that we remove the ID\'s from their original positions
$reordered_ids = array_unique( $post_ids_merged );
// Now we can run our normal query to display 12 posts per page
$args = [
\'post_type\' => \'products\'
\'posts_per_page\' => 12,
\'post__in\' => $reordered_ids,
\'orderby\' => \'post__in\',
\'order\' => \'ASC\',
\'paged\' => get_query_var( \'paged\', 1 ),
];
$loop = new WP_Query( $args );
while( $loop->have_posts() ) {
$loop->the_post();
the_content();
}
wp_reset_postdata();
}
如果你需要这些帖子
贴在每页12篇帖子的顶部
在分页查询中
您可以运行以下两个查询
// Set an array of id\'s to display in front
$move_to_front = [12,13,14,34];
// Run the query to display the posts you need in front
$args_front = [
\'post_type\' => \'products\'
\'posts_per_page\' => count( $move_to_front ),
\'post__in\' => $move_to_front,
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'_price\',
\'order\' => \'ASC\',
];
$loop_front = new WP_Query( $args_front );
if( $loop_front->have_posts() ) {
while( $loop_front->have_posts() ) {
$loop_front->the_post();
the_content();
}
wp_reset_postdata();
}
// Now we can run our major loop to display the other posts
$args = [
\'post_type\' => \'products\'
\'posts_per_page\' => 12,
\'post__not_in\' => $move_to_front,
\'orderby\' => \'meta_value_num\',
\'meta_key\' => \'_price\',
\'order\' => \'ASC\',
\'paged\' => get_query_var( \'paged\', 1 ),
];
$loop = new WP_Query( $args );
if( $loop->have_posts() ) {
while( $loop->have_posts() ) {
$loop->the_post();
the_content();
}
wp_reset_postdata();
}
如果你只需要一页前面有这些帖子,那么@birgire的解决方案就可以很好地完成这项工作