我正在运营一个WordPress网站,大约有35000个海关职位。每个帖子都有一个生日值日期(格式为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,
'post_type' => 'people',
'meta_key' => 'birthday',
'meta_value' => $birthday_date,
'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;