我正在尝试创建一个事件页面,列出一周内的事件(自定义帖子类型)。我很难正确地选择只有“start\\u date”在本周内的帖子。
这是我的代码:
$day = date(\'w\');
$week_start = date(\'d-m-Y\', strtotime(\'-\'. $day . \' days\'));
$week_end = date(\'d-m-Y\', strtotime(\'+\' . (6 - $day) . \' days\'));
$args = array(
\'post_type\' => \'hcc_events\',
\'order\' => \'ASC\',
\'meta_key\' => \'start_date\',
\'orderby\' => \'meta_value\',
\'posts_per_page\' => -1,
\'meta_query\' => array(
\'relation\' => \'AND\',
array(
\'key\' => \'start_date\',
\'value\' => $week_start,
\'compare\' => \'>=\',
\'type\' => \'DATE\'
),
array(
\'key\' => \'start_date\',
\'value\' => $week_end,
\'compare\' => \'<=\',
\'type\' => \'DATE\'
)
)
);
现在什么都没有输出。感谢您的帮助!
SO网友:Oussama Khoubran
这是wordpress提供的简单方法:
$loop = new WP_Query(
array(
\'post_type\' => \'custom_post_type\',
\'posts_per_page\' => 7,
\'meta_key\' => \'meta_key\',
\'orderby\' => \'meta_value_num\',
\'date_query\' => array(
\'date_query\' => array(
array(
\'year\' => date(\'Y\'),
\'week\' => date(\'W\'),
),
),
),
)
);