我搜索了WP Codex和StackExchange并获得了一些线索,但我无法使此查询正常工作。我有两个与Y-m-d格式的每个展览相关的自定义字段:exstart date是展览的开始日期,exend date是结束日期。
我可以轻松地显示即将展出的展品和过去的展品,但我无法获得meta\\U查询权限来显示当前展品(开始日期小于或等于今天,结束日期大于或等于今天)。下面的代码在页面上不显示任何内容。帮助
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$today = date(\'Y-m-d\', strtotime(\'-6 hours\'));
query_posts(array(
\'post_type\' => \'exhibitions\',
\'posts_per_page\' => 6,
\'paged\' => $paged,
\'orderby\' => \'title\',
\'order\' => \'DESC\',
\'meta_query\'=>array(
\'relation\'=>\'AND\',
array(
\'key\' => \'exstart-date\',
\'value\' => $today,
\'compare\' => \'<=\',
\'type\' => \'CHAR\'
),
array(
\'key\' => \'exend-date\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'CHAR\'
)
)
));
if (have_posts()) :
while (have_posts()) : the_post();
最合适的回答,由SO网友:Ray Gulick 整理而成
这是我最终得到的有效代码。我应该提到这个查询在循环中,因为当我把它展示给Mindshare Studios的Damian Taggart时,他注意到他说我应该使用WP\\u query而不是query\\u posts。感谢米洛和其他人在没有所有必要信息的情况下试图帮助我。
<?php
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$today = date(\'Y-m-d\', strtotime(\'-6 hours\'));
$myquery = new WP_Query(array(
\'post_type\' => \'exhibitions\',
\'posts_per_page\' => 6,
\'paged\' => $paged,
\'orderby\' => \'title\',
\'order\' => \'ASC\',
\'meta_query\'=>array(
\'relation\'=>\'AND\',
array(
\'key\' => \'exstart-date\',
\'value\' => $today,
\'compare\' => \'<=\',
\'type\' => \'CHAR\'
),
array(
\'key\' => \'exend-date\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'CHAR\'
)
)
));
if ($myquery->have_posts()) :
while ($myquery->have_posts()) : $myquery->the_post();
?>