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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)
  • ¥15 机器人轨迹规划相关问题