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


how to order on post_IDs key not on value using WP_Query

我像这样将post_ids传递给WP_Query()

$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
        );

$result_post_ids数组是这样的

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) 

所以这是我想要查看的id的顺序,但是当传递$args给WP_Query()时,对值重新排序帖子id,所以他首先显示具有更大值的帖子,像这样

 177 , 176 , ...

但是我想按照键的顺序显示0 , 1 , 2,请提供任何帮助,并提前感谢。

您应该将orderby参数更改为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
);

取自《古抄本》:

'post__in' -保留post__in数组中给定的post ID顺序(从3.5版开始可用)