如何解决比较Wordpress Wp_Query中的日期值
我正在运行一个WordPress网站,其中包含约35 000个海关职位。 每个帖子都有一个生日起息日(格式为DD / MM / YYYY),存储在名为生日的ACF字段中
示例:
$birtday_date = get_field("birthday"); //example 02/07/1955
我想运行2个查询,以便将每个帖子的生日值与我的wordpress中的34999个其他生日日期值进行比较,计算并显示老年人的数量。 这是我的代码,但是由于我不太了解如何比较值而无法使用
您能帮我吗?
<?php
$args = array( // args and query to look for older people that $birthday_date
'posts_per_page' => -1,'post_type' => 'people','meta_key' => 'birthday','meta_value' => $birthday_date,'compare' => '>','type' => 'DATE'
);
$query = new WP_Query($args);
$superior = $query->found_posts;
wp_reset_query();
$args2 = array( // args and query to look for younger people that $birthday_date
'posts_per_page' => -1,'compare' => '<','type' => 'DATE'
);
$query2 = new WP_Query($args2);
$inferior = $query2->found_posts;
wp_reset_query();
echo $superior; // should display the number of older people but display number of people born the same day than $birthday_date
echo $inferior; // should display the number of younger people but display number of people born the same day than $birthday_date
感谢您的帮助。
致谢。
解决方法
您这里可能只需要一个实际查询,我建议您只使用get_posts,因为您不会循环任何东西。您可以只计算与帖子类型中的帖子总数之差。
<?php
$birthday_date = get_field("birthday");
// Count the total number of posts in the post type and get the ones that are published. Note this shorthand only works in PHP5
$total_posts = wp_count_posts('people')->publish;
// Use this for PHP4
// $count_posts = wp_count_posts('people');
// $total_posts = $count_posts->publish;
$args = array( // args and query to look for older people that $birthday_date
'posts_per_page' => -1,'post_type' => 'people','meta_query' => array(
'key' => 'birthday','value' => $birthday_date,'compare' => '>','type' => 'DATE'
)
);
// Get the posts with the older birthdays
$older_birthdays = get_posts($args);
// Count the number of posts in the older birthdays
$num_of_older_bdays = count($older_birthdays);
// Calculate the other birthdays remaining
$num_of_younger_bdays = $total_posts - $older_birthdays;
// Display your birthday counts
echo $num_of_older_bdays;
echo $num_of_younger_bdays;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。