我想使用2个元键筛选query\\u poststart_date
和end_date
, 我想让我的帖子按即将发生的事件和过去的事件来划分。start_date
和end_date
以时间戳格式存储
以下是我尝试过的方法,但不起作用:
$metaq = array(
\'key\' => \'start_date\',
\'value\' => strtotime("today"),
\'compare\' => \'>=\'
);
$args = array(\'category_name\' => \'events\',
\'order\' => \'desc\',
\'orderby\' => \'meta_value\',
\'meta_query\' => $metaq);
query_posts($args);
但它只显示所有类别为
events
.
我想要的是通过使用start_date
和end_date
每一篇尚未开始的帖子都被归类为即将到来的事件。每一篇已经结束的帖子都被归类为过去的事件
SO网友:TheDeadMedic
如前所述,您的查询参数有点偏离-meta_query
should be an array of arrays:
$query = new WP_Query( array(
\'category_name\' => \'events\',
\'order\' => \'DESC\',
\'orderby\' => \'meta_value\',
\'meta_query\' => array(
array(
\'key\' => \'start_date\',
\'value\' => strtotime( \'today\' ),
\'compare\' => \'>=\',
\'type\' => \'UNSIGNED\', // Ensure MySQL treats the value as numeric
)
)
) );
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
// Your template code
}
}