如何通过帖子ID按照与数组相同的顺序获取帖子?
我试过了:
<?php
$ids = array(548,555,587,583,585);
$my_query = query_posts(array('post__in' => $ids));
global $post;
foreach ($my_query as $post){
$posts_by_id[$post->ID] = $post;
}
foreach ($ids as $id) {
if (!$post = $posts_by_id[$id]) continue;
setup_postdata($post);
//do something
echo '<p>TITLE: ';the_title();echo ' - ';the_ID(); '</p>';
the_content();
}
?>
答案 0 :(得分:2)
首先 - 您可能应该使用WP_Query(除非您有一个非常具体的用例)。
http://codex.wordpress.org/Class_Reference/WP_Query
要订购帖子,您可以使用:
array('post__in' => $ids, 'orderby' => 'post__in'))
http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
注意:这仅限于WP 3.5