排序依据自定义字段值不适用于ACF日期字段

时间:2017-11-13 作者:Yamu

我有自定义的post类型事件,它具有自定义字段event\\u date。此字段的日期格式设置为Ymd。

我正在尝试按event\\u日期的顺序获取帖子

以下是按此顺序拉柱的代码

$today = date(\'Ymd\');

$args = array (
    \'post_type\'             => array( \'event\' ),
    \'posts_per_page\'        => \'-1\',
    \'order\'                 => \'ASC\',
    \'orderby\'              =>\'meta_value\', // also tried having meta_value_num but that didn\'t worked as well
    \'meta_key\'              =>\'event_date\',
    \'meta_query\' => array(
        array(
            \'key\'       => \'event_date\',
            \'compare\'   => \'>=\',
            \'value\'     => $today,
        )
    ),
    \'suppress_filters\' => true
);
/*
global $wp_filter;
var_dump( $wp_filter[\'posts_orderby\'] );die;
This returns NULL as well
*/


    $query = new WP_Query( $args );
    echo \'<pre>\';
    var_dump($query->request);die;
但是$query->请求将订单显示为post\\u date,比我提供的查询快。这是请求的转储

string(681) "SELECT   wp_posts.* FROM wp_posts  INNER JOIN wp_postmeta ON ( wp_posts.ID = wp_postmeta.post_id )  INNER JOIN wp_postmeta AS mt1 ON ( wp_posts.ID = mt1.post_id ) WHERE 1=1  AND ( 
  wp_posts.ID NOT IN (
                SELECT object_id
                FROM wp_term_relationships
                WHERE term_taxonomy_id IN (11)
            )
) AND ( 
  wp_postmeta.meta_key = \'event_date\' 
  AND 
  ( 
    ( mt1.meta_key = \'event_date\' AND mt1.meta_value >= \'20171113\' )
  )
) AND wp_posts.post_type = \'event\' AND (wp_posts.post_status = \'publish\' OR wp_posts.post_status = \'acf-disabled\' OR wp_posts.post_status = \'private\') GROUP BY wp_posts.ID ORDER BY wp_posts.post_date ASC "
无法按event\\u日期排序。我做错了什么??甚至尝试过

remove_all_filters(\'posts_orderby\');
之前的wp\\u查询结果仍然相同。

注意:在stackoverflow中发布了此内容,但无法获得答案,因此这是一个重新发布。

1 个回复
SO网友:piers1

Wordpress构建查询时似乎存在错误。我今天使用ACF Pro也遇到了同样的问题。我向orderby传递了一个数组,它成功了。这是我的代码:

$args = [
        \'post_type\'         => \'event\',
        \'meta_query\'        => array(
                    \'key\'       => \'event_date\',
                    \'value\'     => date(\'Ymd\'),
                    \'type\'      => \'DATE\',
                    \'compare\'   => \'>=\'
        ),
        \'meta_key\'  => \'event_date\',
        \'orderby\' => array( \'meta_value\' => \'ASC\' ), 
    ];

结束

相关推荐