【问题标题】:how to order on post_IDs key not on value using WP_Query如何使用 WP_Query 在 post_IDs 键上排序而不是值
【发布时间】:2015-09-15 22:32:49
【问题描述】:

我像这样将 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 请任何帮助,提前非常感谢。

【问题讨论】:

    标签: php mysql wordpress


    【解决方案1】:

    您应该将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
    );
    

    来自the Codex

    'post__in' - 保留post__in 数组中给定的帖子 ID 顺序(自版本 3.5 起可用)

    【讨论】:

      猜你喜欢
      • 2021-05-24
      • 2012-01-28
      • 2016-12-30
      • 1970-01-01
      • 1970-01-01
      • 2020-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多