我的情况:
我有两种自定义帖子类型:
office
和employee
。借助 ACF 关系字段,您可以在编辑 office
帖子类型时选择员工。
employee
帖子类型有一个术语称为availability
(术语:是,否)。
我想过滤我的办公室查询,仅在选择“是”一词时显示正确的员工。
到目前为止,我所拥有的是下面的代码,用于过滤分配到办公室的员工。如何添加根据
employee
帖子类型过滤掉的最后一部分?
$args = array(
'post_type' => 'office',
'posts_per_page' => -1,
'order' => 'ASC'
);
$args['meta_query'][] = array(
'key' => 'acf_employee_relationship_field',
'value' => $selected_office,
'compare' => 'LIKE',
);
// First, let's get the IDs of employees with the term "availability"
$employees_with_availability = get_posts(array(
'post_type' => 'employee',
'tax_query' => array(
array(
'taxonomy' => 'availability_taxonomy', // Replace with your actual taxonomy name
'field' => 'slug',
'terms' => 'availability',
),
),
'fields' => 'ids', // Only fetch IDs to improve performance
));
// Now, let's query offices connected to these employees
$office_args = array(
'post_type' => 'office',
'meta_query' => array(
array(
'key' => 'office_relation_field', // Replace with your actual ACF field name
'value' => $employees_with_availability,
'compare' => 'IN',
),
),
);
$office_query = new WP_Query($office_args);
// Loop through the query results
if ($office_query->have_posts()) {
while ($office_query->have_posts()) {
$office_query->the_post();
// Display your office data here
// Example: the_title(), the_content(), etc.
}
wp_reset_postdata(); // Reset post data to avoid conflicts
} else {
// No offices found
echo 'No offices found.';
}
这段代码应该可以实现您的目标,我们做了 2 个查询而不是 1 个,但我认为它应该可以解决问题!欲了解更多信息,请检查: