functions - Check if term is in a taxonomy?

admin2025-01-07  3

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!

Share Improve this question asked May 21, 2012 at 19:53 HandiworkNYC.comHandiworkNYC.com 4011 gold badge5 silver badges16 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 2

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' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736260319a657.html

最新回复(0)