我有两个自定义字段与Y-m-d格式的每篇文章关联:start\\u date是文章的开始日期,end\\u date是结束日期。
我可以轻松显示即将发布的帖子、当前帖子和过去的帖子,但我无法获得meta\\U查询权限来显示即将发布的帖子、当前帖子或过去帖子的总帖子数(开始日期小于或等于今天,结束日期大于或等于今天)。下面的代码显示即将发布的帖子:
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$today = date(\'Y-m-d\', strtotime(\'-1 hours\'));
query_posts(array(
\'post_type\' => \'post\',
\'paged\' => $paged,
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_query\'=>array(
\'relation\'=>\'AND\',
array(
\'key\' => \'start_date\',
\'value\' => $today,
\'compare\' => \'<=\',
\'type\' => \'CHAR\'
),
array(
\'key\' => \'end_date\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'CHAR\'
)
)
));
if (have_posts()) :
while (have_posts()) : the_post();
有了这段代码,我可以显示带有日期范围的当前帖子,但我想打印帖子总数。
请帮忙?
最合适的回答,由SO网友:Vincens von Bibra 整理而成
如果您保存query_post(...)
作为变量,然后运行$var->found_posts
, 这会给你带来预期的结果吗?
像这样:
$paged = ( get_query_var(\'paged\') ) ? get_query_var(\'paged\') : 1;
$today = date(\'Y-m-d\', strtotime(\'-1 hours\'));
$query = query_posts(array(
\'post_type\' => \'post\',
\'paged\' => $paged,
\'orderby\' => \'meta_value\',
\'order\' => \'ASC\',
\'meta_query\'=>array(
\'relation\'=>\'AND\',
array(
\'key\' => \'start_date\',
\'value\' => $today,
\'compare\' => \'<=\',
\'type\' => \'CHAR\'
),
array(
\'key\' => \'end_date\',
\'value\' => $today,
\'compare\' => \'>=\',
\'type\' => \'CHAR\'
)
)
));
echo $query->found_posts . " posts.";
if (have_posts()) :
while (have_posts()) : the_post();
如果您在查询中调整比较值,您应该会得到相应的结果?!