Home > database >  Wordpress: check whether the current post is in any of the specified terms
Wordpress: check whether the current post is in any of the specified terms

Time:11-28

I have a custom post type property and a custom taxonomy features. On single property pages, I output all terms from features, including empty ones. However, I need to check which of these terms the current post is present in.

At the exit you need to get something like: ✓ cleaning X Internet ✓ home phone.

So, I get all the terms:

$features = get_terms([
        'taxonomy' => 'features',
        'hide_empty' => false,
    ]);

Then I try to sort them out and check if there is a current post in the term being checked.

foreach ($features as $feature) :
    if (has_term($feature)) {
        echo '✓' . $feature->name;
    } else {
        echo 'X' . $feature->name;
    }
endforeach;

But I get:

X cleaning X Internet X home phone

CodePudding user response:

Ok, I made a mistake, that's how it will work)

foreach ($features as $feature) :
    if (has_term($feature, 'features')) {
        echo '✓' . $feature->name;
    } else {
        echo 'X' . $feature->name;
    }
endforeach;
  • Related