WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/term exists

Description

Check if a given term exists and return the term ID, a term array, or 0 (false) if the term doesn't exist.

Usage

<?php term_exists$term$taxonomy$parent ?>

Parameters

$term
(integer|string) (required) The term to check
Default: None
$taxonomy
(string) (optional) The taxonomy name to use
Default: ''
$parent
(integer) (optional) $parent ID of parent term under which to confine the exists search
Default: 0

Return Values

(mixed)
  • Returns 0 or NULL if the term does not exist.
  • Returns the term ID if no taxonomy was specified and the term exists.
  • Returns an array if the parent exists. (format: array('term_id'=>term id, 'term_taxonomy_id'=>taxonomy id))

Examples

Checks to see if 'Uncategorized' category exists

<?php
$term = term_exists( 'Uncategorized', 'category' );
if ( 0 !== $term && null !== $term ) {
  echo "'Uncategorized' category exists!";
}
?>

Checks to see if 'Untagged' post_tag category exists

<?php
$term = term_exists( 'Untagged', 'post_tag' );
if ( 0 !== $term && null !== $term ) {
  echo "'Untagged' post_tag exists!";
}
?>

Notes

  • Uses global: (object) $wpdb

Change Log

Since: 3.0

Source File

term_exists() is located in wp-includes/taxonomy.php.

Related

See also index of Function Reference and index of Template Tags.