Reputation: 21
I want to check if there is at least one female in the custom post and if there is one I want to set the condition to true: $has_one_female = true;
But it never enters in this if:
foreach($team_members as $team_member) {
if(has_category( 'female', $team_member)) {
$has_one_female = true;
break;
}
}
See complete code below:
$args = array(
'orderby' => 'rand',
'post_status' => 'publish',
'post_type' => 'team',
'posts_per_page' => 6,
'tax_query' => array(
array(
'taxonomy' => 'category-team',
'field' => 'slug',
'terms' => array('team', 'female')
)
)
);
$query = new WP_Query($args);
$team_members = array();
$has_one_female = false;
if($query->have_posts()) {
$team_members = $query->posts;
print_r($query);
$has_one_female = false;
foreach($team_members as $team_member) {
if(has_category( 'female', $team_member)) {
$has_one_female = true;
break;
}
}
}
Upvotes: 0
Views: 1341
Reputation: 328
Would in_category() suffice?
So you would then do something like:
if (in_category('female')) {
}
you could also check an array like:
if (in_category( ['female', 'other'] )) {}
If you would need to check based on a custom taxonomy I think you would be able to use this as well:
if (has_term('female', 'gender_taxonomy_name')) {}
There's a difference in checking categories or taxonomies:
Check post for category — use in_category() Check post for tax term — use has_term()
Upvotes: 1