我正在尝试this 但有多个职位和具体职位。帖子的位置现在是硬编码的,但将从post meta中提取出来。这段代码只在主页上运行,帖子没有分页。
$essay_args = array(
\'posts_per_page\' => get_option(\'posts_per_page\'),
\'post__not_in\' => array($post->ID),
\'post_type\' => array(\'post\')
);
$essays = get_posts( $essay_args );
$fixed_posts = get_posts(array(\'post_type\'=>array(\'ad\',\'book\')));
$positions = array(2,5,10); // to be pulled in from ad/book posts
$x = 0;
foreach($fixed_posts as $fixed_post) : setup_postdata($post);
$e_posts = new WP_Query(array(\'p\'=>$fixed_post->ID,\'post_type\'=>array(\'ad\',\'book\')));
if (!empty($e_posts->posts)) array_splice($essays,$positions[$x],0,$e_posts->posts);
$x++;
endforeach;
foreach($essays as $essay) : setup_postdata($post);
echo $essay->post_title . \'<br>\';
endforeach;
对每个帖子按id执行多个WP\\u Query/get\\u posts请求更有意义吗?还是这会对数据库造成太大的负担?有没有更干净的方法?
SO网友:user49163
您可以尝试重建一个包含两个查询的数组,或者将fixed\\u帖子拼接到随笔数组中。也许是下面这样。
$essay = get_posts();
$fixed_posts = get_posts();
$post_positions = array(2,5,10);
$x = 0;
foreach ($essays as $i => $essay) {
if ($post_positions[$x] == $i) {
$top_posts[] = $fixed_posts[$x];
$x++;
}
$top_posts[] = $essay;
}
// Alternatively
foreach ($fixed_posts as $i => $fixed_post) {
array_splice($essays, $post_positions[$i], 0, $fixed_post);
}