Using WordPress ‘is_term()’ PHP function

The is_term() WordPress PHP function checks if a term exists within a specified taxonomy and under a specified parent term.

Usage

is_term( $term, $taxonomy, $parent );

Parameters

  • $term (int|string) (Required): The term to check.
  • $taxonomy (string) (Optional): The taxonomy name to use. Default: ”.
  • $parent (int) (Required): ID of the parent term under which to confine the exists search.

More information

See WordPress Developer Resources: is_term()

Examples

Check if a term exists in a category

// Check if the term "travel" exists in the "category" taxonomy
$term_exists = is_term('travel', 'category');

Check if a term exists with a specific ID

// Check if a term with the ID 42 exists
$term_exists = is_term(42);

Check if a term exists in a custom taxonomy

// Check if the term "fiction" exists in the "book_genre" taxonomy
$term_exists = is_term('fiction', 'book_genre');

Check if a term exists under a specific parent term

// Check if the term "Europe" exists in the "destination" taxonomy under the parent term with ID 5
$term_exists = is_term('Europe', 'destination', 5);

Check if a term exists and output a message

// Check if the term "cooking" exists in the "category" taxonomy
$term_exists = is_term('cooking', 'category');

// Output a message based on the result
if ($term_exists) {
    echo 'The term "cooking" exists.';
} else {
    echo 'The term "cooking" does not exist.';
}

Tagged in

Leave a Comment

Your email address will not be published. Required fields are marked *