Mysql WordPress以任意顺序获取特定帖子
现在,我正在做:Mysql WordPress以任意顺序获取特定帖子,mysql,wordpress,select,post,sql-order-by,Mysql,Wordpress,Select,Post,Sql Order By,现在,我正在做: $posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7))); 我有两个问题: Post 3是'Post_type'=>'Post',所以它没有被选中,但我想要它!如果我省略了“post\u type”=>“page”,那么只选择post 3,因为它必须采用“post\u type”=>“post”。。 我希望能够。如果我知道如何使用MySQL,我可以: S
$posts = get_posts(array('post_type' => 'page', 'post__in' => array(1, 3, 2, 9, 7)));
我有两个问题:
Post 3是'Post_type'=>'Post',所以它没有被选中,但我想要它!如果我省略了“post\u type”=>“page”,那么只选择post 3,因为它必须采用“post\u type”=>“post”。。
我希望能够。如果我知道如何使用MySQL,我可以:
SELECT * FROM wp_posts WHERE ID IN (1, 3, 2, 9, 7)
ORDER BY FIND_IN_SET(ID, '1,3,2,9,7');
但是,我应该如何使用它呢?首先通过它们的ID任意获取所有帖子,然后循环遍历所有帖子 您可以这样做:-
$posts=$wpdb->get_results("SELECT ID FROM $wpdb->posts WHERE ID IN (1, 3, 2, 9, 7)
ORDER BY FIND_IN_SET(ID, '1,3,2,9,7')");
$count=count($posts);
for ($counter=0 ; $counter < $count; $counter++)
{
$post=get_post( $posts[$counter]->ID, $output );
//do your stuffs with posts
}
希望这有帮助
wordpress IRC频道上的Kawauso告诉我post_类型接受一个值数组。因此,我发现以下内容也选择了第3篇文章:
因此,我做了以下工作:
$post_ids = array(1 => 0, 3 => 1, 2 => 2, 9 => 3, 7 => 4);
$posts = get_posts(array('post_type' => array('post', 'page'),
'post__in' => array_keys($post_ids)));
$ordered_posts = array(0,0,0,0,0); // size of five; keeps order
foreach ($posts as $p) {
setup_postdata($p);
$ordered_posts[$post_ids[$p->ID]] = array(
'permalink' => get_permalink($p->ID),
'title' => $p->post_title,
'excerpt' => get_the_excerpt(),
'date' => date('F j, Y', strtotime($p->post_date)));
}
根据,您可以使用此代码,然后使用经典的WordPress循环:
$args = array(
'post_type'=>'page',
'orderby'=>'menu_order',
'order'=>'ASC'
);
query_posts($args);
这是可行的,但我认为您正在进行过多的SQL查询。好了,get_post执行SQL查询。因此,您可以将第一行替换为$order=>array1,3,2,9,7。但是你仍然要做五个SQL查询,每个帖子一个,而且在大多数情况下,foreach更快更干净。看这个。这很好知道,但在这种情况下,它们不是所有的页面,我认为它们的顺序不同于菜单顺序。