我有一个自定义元字段“bhaa\\u runner\\u dateofrenewal”,它记录了我系统上用户上次更新其成员资格的日期,字段格式为YYYY-MM-DD。我正在尝试编写一个自定义查询,该查询将按年份查询和排序返回的用户列表。
我遇到了这个custom-field-multiple-clauses 文章概述了如何在值字段中使用“date()”函数
$args = array(
\'posts_per_page\' => 100,
\'post_type\' => \'create_events\',
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'startDate\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'>=\'
),
array(
\'key\' => \'endDate\',
\'value\' => date(\'Y-m-d\'),
\'compare\' => \'<=\'
),
\'orderby\' => \'meta_value\',
\'meta_key\' => \'startDate\',
\'order\' => \'DESC\'
)
);
$custom_query = new WP_Query( $args );
我的自定义查询目前看起来是这样的,但它使用了“bhaa\\u runner\\u dateofrenewal”字段值的类似字符串的比较。
$usersByCompanyArgs = new WP_User_Query(
array(
\'fields\' => \'all_with_meta\',
\'orderby\' => \'bhaa_runner_dateofrenewal\',
\'order\' => \'DESC\',
\'meta_query\' => array(
array(
\'key\' => \'bhaa_runner_company\',
\'value\' => $this->houseid,
\'compare\' => \'=\'),
array(
\'key\' => \'bhaa_runner_dateofrenewal\',
\'value\' => \'2014\',
\'compare\' => \'LIKE\')
)
)
);
有人知道我如何更新子句,以便将字段视为真实日期,并在此处使用自定义MYSQL YEAR()函数吗?
最合适的回答,由SO网友:fischi 整理而成
您可以在meta_query
.
不幸的是,您只能使用比较BETWEEN
如果您的日期符合以下格式YYYYMMDD
. 因此,要查看一年的用户,您必须如下设置元查询:
$usersByCompanyArgs = new WP_User_Query(
array(
\'fields\' => \'all_with_meta\',
\'orderby\' => \'bhaa_runner_dateofrenewal\',
\'order\' => \'DESC\',
\'meta_query\' => array(
array(
\'key\' => \'bhaa_runner_company\',
\'value\' => $this->houseid,
\'compare\' => \'=\'),
array(
\'key\' => \'bhaa_runner_dateofrenewal\',
\'value\' => \'2014-01-01\', // date to compare to, before this one
\'compare\' => \'<\',
\'type\' => \'DATE\' //set the format
),
array(
\'key\' => \'bhaa_runner_dateofrenewal\',
\'value\' => \'2013-01-01\', // date to compare to, after this one
\'compare\' => \'>=\',
\'type\' => \'DATE\' //set the format
),
)
)
);
如果您的日期字段已格式化
YYYYMMDD
您可以替换
meta_query
具有
array(
\'key\' => \'bhaa_runner_dateofrenewal\',
\'value\' => array( \'20130101\', \'20140101\' ), // date to compare to, after this one
\'compare\' => \'BETWEEN\',
\'type\' => \'DATE\' //set the format
)