Wordpress 按帖子 id 按数组中的确切顺序获取帖子

1 wordpress

如何按照与数组中相同的顺序按帖子 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();

}

?>
Run Code Online (Sandbox Code Playgroud)

Ste*_*nes 6

首先 - 您可能应该为此使用 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 起