我试图按元值排序wp_查询
我使用以下参数

array(
  'post_type'      =>'event',
  'posts_per_page' => -1,
  'meta_query'     => array(
  'dates_query' => array(
          'key' => 'dates',
          'value' => date(time()),
          'compare' => '>='
          )
       ),
  'orderby'        => 'dates_query',
  'order'          => 'ASC',
  'paged'          => 1
);

这是wp_query生成的请求
SELECT wp_posts.* FROM wp_posts
INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )
WHERE 1=1 AND ( ( wp_postmeta.meta_key = 'dates'
         AND wp_postmeta.meta_value >= '1514960717' ) )
         AND wp_posts.post_type = 'event'
         AND (wp_posts.post_status = 'publish'
         OR wp_posts.post_status = 'completed'
         OR wp_posts.post_status = 'acf-disabled')
         GROUP BY wp_posts.ID
         ORDER BY CAST(wp_postmeta.meta_value AS CHAR) ASC

不幸的是,结果是错误的,当尝试手动执行查询时,我得到以下错误:
Expression #1 of ORDER BY clause is not in GROUP BY clause and contains non-aggregated column

我知道有一个选项可以禁用“only_full_group_by”,但我想知道在这种情况下这是否是最佳实践
谢谢

最佳答案

不知道为什么要使用数据查询,但应该在键值比较时直接使用元查询。如果有用的话告诉我。

array(
    'post_type'     => 'event',
    'posts_per_page'=> -1,
    'paged'          => 1,
    'meta_key'      => 'dates',
    'orderby'       => 'meta_value',
    'order'         => 'ASC',
    'meta_query'    => array(
            array(
                'key'       => 'dates',
                'value'     =>  date(time()), //<-- Cross check if your date has same format.
                'compare'   => '>=',
                'type'      => 'DATE'
            )
    )
);

10-04 16:55