Is there a function that does something like:
is_in_taxonomy($term, $taxonomy)
Where it returns true if $taxonomy
has that $term
. ????
Thanks!
Is there a function that does something like:
is_in_taxonomy($term, $taxonomy)
Where it returns true if $taxonomy
has that $term
. ????
Thanks!
You're probably looking for term_exists()
:
<?php term_exists( $term, $taxonomy, $parent ); ?>
$term
is required (obviously).
Both $taxonomy
and $parent
are optional, but if you want to determine if a specific taxonomy has a given term, just pass the registered taxonomy name via the $taxonomy
parameter.
term_exists
term_exists( $term, $taxonomy );
Here you can use term_exists to ensure term exits or not if not than insert it and set with your post
$term = term_exists( $termName, '$taxonomyName' );
if( ! $term ) {
$term = wp_insert_term( $termName, '$taxonomyName' );
}
$term_id = is_array( $term ) ? $term['term_id'] : $term;
wp_set_post_terms( $postId, $term_id,'$taxonomyName' );