[英]get month from date meta query in WP_Query
I store a date type stored by ACF Pro in custom meta "event_date" and I want to filter event by months with a query. 我将ACF Pro存储的日期类型存储在自定义元“ event_date”中,并且我想通过查询按月过滤事件。 The data is stored in
数据存储在
I know that I need to use meta_query array like this : 我知道我需要像这样使用meta_query数组:
$args['meta_query'] = array(
array(
'key' => 'event_date',
'value' => ?,
'compare' => '=',
'type' => 'DATE'
)
);
But the meta query
allows to compare with a full date. 但是
meta query
允许与完整日期进行比较。 How to compare if the month variable (number 1 to 12) is the same that the event_date
meta month ? 如何比较month变量(1到12)是否与
event_date
元月份相同?
Thanks 谢谢
Assuming that you are using ACF's default date 'save format' which is 'yymmdd'. 假设您使用的是ACF的默认日期“保存格式”,即“ yymmdd”。 Try the following 'meta query'.
请尝试以下“元查询”。
FOR MONTH: 每月:
$month = date('m'); // Change to static if not current month (should be 2 digits)
$start_date = date('Y'.$month.'01'); // First day of the month
$end_date = date('Y'.$month.'t'); // Last day of the month
$args['meta_query'] = array(
array(
'key' => 'event_date',
'value' => array($start_date, $end_date),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
);
FOR YEAR: 一年:
$year = date('Y'); // Change to static if not current year (should be 4 digits)
$start_date = date($year.'0101'); // First day of the year
$end_date = date($year.'1231'); // Last day of the year
$args['meta_query'] = array(
array(
'key' => 'event_date',
'value' => array($start_date, $end_date),
'compare' => 'BETWEEN',
'type' => 'DATE'
)
);
NOTE: Instead of
'type' => 'DATE'
, you can use'type' => 'NUMERIC'
too.注意:也可以使用
'type' => 'NUMERIC'
代替'type' => 'DATE'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.