WP_Query,带有按数组排序的ID数组

时间:2014-03-07 04:31:29

标签: wordpress sorting

我有一系列ID,我想通过WP_Quey()

获取这些帖子
$myarray = $ids;
$args = array( 'post__in' => $myarray);
// The Query
$the_query = new WP_Query( $args );

它,按日期排序结果但是我想通过$ myarray项目对它进行排序,第一个结果将是$ myarray中的第一个ID

3 个答案:

答案 0 :(得分:28)

在Wordpress 3.5及更高版本中,你可以使用'orderby'=>'post__in',然后你必须写下这个:

$myarray = $ids;    
$args = array('post__in'=> $myarray, 'orderby'=>'post__in');
// The Query
$the_query = new WP_Query( $args );

答案 1 :(得分:0)

我知道,我的答案为时已晚,但我必须正确回答。

我试过'orderby'=>'post__in'

例如,我已经动态更新了cookie,我必须按顺序在最近查看的产品块中加载产品,必须最后查看该块中的第一个产品。

行。

我有ids 1720,19626,19173,19188

$args = array('post__in'=> $myarray, 'orderby'=>'post__in');

输出中的此字符串按顺序返回我的产品:

19626,19188,19173,1720 ,这不是我的订单。这导致默认订单参数 DESC WP_Query。我们只有另外一次机会 - ASC它...... M H非常悲伤的答案。

我的回答很简单:

我们不需要' orderby' =>' post__in'

我们必须得到:

$myarray = $ids;    
$args = array('post__in'=> $myarray);
$the_query = new WP_Query( $args );

之后我们做:

foreach($myarray as $myarray_id){
 while ( $the_query->have_posts()) {
   $the_query->the_post();
   if($post->ID == $myarray_id){
     //DO SOMETHING
   }
 }
}

那就是它!

答案 2 :(得分:-2)

正确传送订单:

$id_array = array(5,2,3,7);

foreach($id_array as $id_array_v)
{
   $query = new WP_Query(array('post__in' => array($id_array_v)));

   while($query -> have_posts())
   {
      $query -> the_post();
      the_title();
   }
}
相关问题