我有一个令人沮丧的问题。我正在使用date_query
如法典所述:
<?php
$today = getdate();
$args = array(
\'post_type\' => \'Lighting\',
\'post_status\' => \'publish\',
\'posts_per_page\' => 1,
\'date_query\' => array(
array(
\'year\' => $today[\'year\'],
\'month\' => $today[\'mon\'],
\'day\' => $today[\'mday\'],
),
),
);
$the_query = new WP_Query( $args );
如果排除,则可以按预期显示自定义帖子
\'day\'
, 当它包含在查询中时,我没有收到任何帖子。我正在使用高级自定义字段Pro
date-picker
. 我不知道为什么会发生这种情况,我一直在不知疲倦地寻找
date_query
不工作。我能回显日期,所以我不知道断开的地方在哪里。
/******对答案的回应******/
感谢您的回复。我已经用我认为合适的日期格式进行了meta\\u查询,但我仍然无法仅查询今天的帖子。以下是新查询:
<?php // Let\'s get the data we need to loop through below
$args = array(
\'post_type\' => \'carillon\', // Tell WordPress which post type we want
\'orderby\' => \'meta_value\', // We want to organize the events by date
\'meta_key\' => \'date_of_lighting\', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
\'posts_per_page\' => \'1\', // Let\'s show just one post.
\'meta_query\' => array( // WordPress has all the results, now, return only the event on today\'s date
array(
\'key\' => \'date_of_lighting\', // Check the s"date_of_lighting field
\'value\' => date("Y-M-D"), // Set today\'s date (note the similar format)
\'compare\' => \'=\', // Return only today\'s post
\'type\' => \'NUMERIC\' // Let WordPress know we\'re working with numbers
)
)
);
有什么建议吗?再次感谢。
/********解决方案**********/
大家好,
所以,我找到了一个可行的解决方案。我将类型更改为“日期”。有趣的是,在其他示例中,人们希望显示今天及以后的日期,他们使用“数字”类型。我想这是有道理的,但我将跳进法典来进一步理解这一点。我感谢你的帮助。以下是有效的解决方案:
<?php // Let\'s get the data we need to loop through below
$args = array(
\'post_type\' => \'carillon\', // Tell WordPress which post type we want
\'orderby\' => \'meta_value\', // We want to organize the events by date
\'meta_key\' => \'date_of_lighting\', // Grab the "date_of_event" field created via "date-picker" plugin (stored in ‘YYYYMMDD’)
\'posts_per_page\' => \'1\', // Let\'s show just one post.
\'meta_query\' => array( // WordPress has all the results, now, return only the event for today\'s date
array(
\'key\' => \'date_of_lighting\', // Check the s"date_of_lighting field
\'value\' => date("Y-m-d"), // Set today\'s date (note the similar format)
\'compare\' => \'=\', // Return only today\'s post
\'type\' => \'DATE\' // Let WordPress know we\'re working with a date
)
)
);