我有一个自定义字段,它将日期存储在时间戳中。我想运行一个查询,显示基于月份的帖子,例如从三月开始的所有条目,具体日期或年份无关紧要。我想我需要为元查询使用DATE或DATETIME类型,但我不知道如何继续:
if ($_GET[\'month\']) {
$meta_query[] = array(
\'key\' => \'event_start_date\',
\'value\' => $_GET[\'month\']
);
}
$args = array(
\'post_type\' => \'event\',
\'paged\' => $paged,
\'posts_per_page\' => -1,
\'tax_query\' => $cleanArray,
\'meta_key\' => \'event_start_date\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\',
\'meta_query\'=> $meta_query
);
$events = new WP_Query($args);
最合适的回答,由SO网友:eskimo 整理而成
您需要查询的两位数月份号码,然后使用下面的代码。使用php应该很容易做到这一点(例如,请参见this post). 在下面的代码中$month
是2位数格式的月份数,例如3月是03。
$start_date = date(\'Y\'.$month.\'01\'); // First day of the month
$end_date = date(\'Y\'.$month.\'t\'); // \'t\' gets the last day of the month
$meta_query = array(
\'key\' => \'event_start_date\',
\'value\' => array($start_date, $end_date),
\'compare\' => \'BETWEEN\',
\'type\' => \'DATE\'
);
SO网友:Mhar Daniel
对于日期时间,只需添加eg。00:00:00 - 23:00:00
$month = $_GET[\'month\'];
$start_date = date( \'Y-\' . $month . \'-01 00:00:00\' );
$end_date = date( \'Y-\' . $month . \'-t 23:00:00\', strtotime( $start_date ) );
$meta_query = array(
\'key\' => \'event_start_date\',
\'value\' => array($start_date, $end_date),
\'compare\' => \'BETWEEN\',
\'type\' => \'DATETIME\'
);