drbuowqe02101 2015-09-15 14:25
浏览 38
已采纳

如何使用WP_Query对post_IDs键而不是值进行排序

i'm passing post_ids to WP_Query() like this

$result_post_ids = $wpdb->get_results("SELECT  DISTINCT auction_id 
                   FROM " . $wpdb->prefix . "simple_auction_log 
                   WHERE userid = $user_id 
                   order by date DESC" , ARRAY_N);

  $args = array (
            'post__in' => $result_post_ids ,
            'post_type' => 'product' ,
            'posts_per_page' => $limit ,
            'paged' => $page ,
            'tax_query' => $category_filter , //category
            'orderby' => $order_by,
            'order' => $order
        );

the $result_post_ids array is like this

array (size=15)
  0 => string '175' (length=3)
  1 => string '148' (length=3)
  2 => string '169' (length=3)
  3 => string '176' (length=3)
  4 => string '170' (length=3)
  5 => string '205' (length=3)
  6 => string '142' (length=3)
  7 => string '168' (length=3)
  8 => string '132' (length=3)
  9 => string '173' (length=3)
  10 => string '177' (length=3)
  11 => string '84' (length=2)
  12 => string '171' (length=3)
  13 => string '128' (length=3)
  14 => string '82' (length=2) 

so this is the order of ids i want to view but when passing the $args to WP_Query() is reorder the post ids on the value so he is displaying the post with the grater value first like this

 177 , 176 , ...

but i want to display on the order of the keys 0 , 1 , 2 please any help and many thanks in advance.

  • 写回答

1条回答 默认 最新

  • douzi9744 2015-09-15 14:32
    关注

    You should change your orderby parameter to post__in:

    $args = array (
        'post__in' => $result_post_ids ,
        'post_type' => 'product' ,
        'posts_per_page' => $limit ,
        'paged' => $page ,
        'tax_query' => $category_filter ,
        'orderby' => 'post__in',  // Order by the array passed in above
        'order' => $order
    );
    

    From the Codex:

    'post__in' - Preserve post ID order given in the post__in array (available since Version 3.5)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示