我有一个称为事件的自定义帖子类型,有两个自定义字段,开始日期和结束日期。这些字段使用时间戳存储在数据库中。我想基于这两个值创建一个自定义查询,以便列出指定日期/时间范围内的事件。我的代码是这样的:
$start_date = strtotime($_POST[\'start_date\']);
$end_date = strtotime($_POST[\'end_date\']);
$args = array(
\'post_type\' => \'event\',
\'posts_per_page\' => -1,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'meta_query\'=>array(
\'relation\'=>\'AND\',
array(
\'key\' => \'event_start_date\',
\'value\' => $start_date,
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'event_end_date\',
\'value\' => $end_date,
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
)
)
);
$events = new WP_Query($args);
所以我使用了一个简单的数字meta\\u查询,因为我的所有值和键都存储在一个时间戳中。代码对我来说很好,但不知怎么的,这个查询中没有结果。以下是一个示例:
$star\\u date=1343779200$end\\u date=141212600
我的一篇事件帖子将这些值作为自定义字段:event\\u start\\u date=1375315200event\\u end\\u date=1377734400
因此,它应该至少给出一个结果,因为与event\\u start\\u date和event\\u end\\u date相比,start\\u date更小,end\\u date更高。
知道怎么了吗?
SO网友:jay praksh
$today2 = date(\'Ymd\');
$current_month = date(\'m\');
$current_year = date(\'Y\');
$today1 = $_GET[\'event_date\'];
//echo $xx = substr($today1,0,4);
$current_month = substr($today1,-4,2);
$current_year = substr($today1,0,4);
//echo \'get-->\'.$today1;
//$today = ($today1 == \'\' || NULL )? $today2 : $today1;
$args = array(
\'post_type\' => \'aw_events\',
\'posts_per_page\' => $posts_per_page,
\'paged\' => $paged,
\'meta_query\' => array(
//\'relation\' => \'AND\',
array(
\'key\' => \'event_start_date\',
\'compare\' => \'<=\',
\'value\' => $current_year.\'\'.$current_month.\'30\',
),
array(
\'key\' => \'event_end_date\',
\'compare\' => \'>=\',
\'value\' => $current_year.\'\'.$current_month.\'01\',
),
),
);