term_exists( int|string $term, string $taxonomy = '', int $parent = null )
Determines whether a term exists.
Description Description
Formerly is_term(), introduced in 2.3.0.
For more information on this and similar theme functions, check out the Conditional Tags article in the Theme Developer Handbook.
Parameters Parameters
- $term
-
(int|string) (Required) The term to check. Accepts term ID, slug, or name.
- $taxonomy
-
(string) (Optional) The taxonomy name to use
Default value: ''
- $parent
-
(int) (Optional) ID of parent term under which to confine the exists search.
Default value: null
Return Return
(mixed) Returns null if the term does not exist. Returns the term ID if no taxonomy is specified and the term ID exists. Returns an array of the term ID and the term taxonomy ID if the taxonomy is specified and the pairing exists.
Source Source
File: wp-includes/taxonomy.php
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 | function term_exists( $term , $taxonomy = '' , $parent = null ) { global $wpdb ; $select = "SELECT term_id FROM $wpdb->terms as t WHERE " ; $tax_select = "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE " ; if ( is_int ( $term ) ) { if ( 0 == $term ) { return 0; } $where = 't.term_id = %d' ; if ( ! empty ( $taxonomy ) ) { return $wpdb ->get_row( $wpdb ->prepare( $tax_select . $where . ' AND tt.taxonomy = %s' , $term , $taxonomy ), ARRAY_A ); } else { return $wpdb ->get_var( $wpdb ->prepare( $select . $where , $term ) ); } } $term = trim( wp_unslash( $term ) ); $slug = sanitize_title( $term ); $where = 't.slug = %s' ; $else_where = 't.name = %s' ; $where_fields = array ( $slug ); $else_where_fields = array ( $term ); $orderby = 'ORDER BY t.term_id ASC' ; $limit = 'LIMIT 1' ; if ( ! empty ( $taxonomy ) ) { if ( is_numeric ( $parent ) ) { $parent = (int) $parent ; $where_fields [] = $parent ; $else_where_fields [] = $parent ; $where .= ' AND tt.parent = %d' ; $else_where .= ' AND tt.parent = %d' ; } $where_fields [] = $taxonomy ; $else_where_fields [] = $taxonomy ; if ( $result = $wpdb ->get_row( $wpdb ->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $where AND tt.taxonomy = %s $orderby $limit" , $where_fields ), ARRAY_A ) ) { return $result ; } return $wpdb ->get_row( $wpdb ->prepare( "SELECT tt.term_id, tt.term_taxonomy_id FROM $wpdb->terms AS t INNER JOIN $wpdb->term_taxonomy as tt ON tt.term_id = t.term_id WHERE $else_where AND tt.taxonomy = %s $orderby $limit" , $else_where_fields ), ARRAY_A ); } if ( $result = $wpdb ->get_var( $wpdb ->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $where $orderby $limit" , $where_fields ) ) ) { return $result ; } return $wpdb ->get_var( $wpdb ->prepare( "SELECT term_id FROM $wpdb->terms as t WHERE $else_where $orderby $limit" , $else_where_fields ) ); } |
Expand full source code Collapse full source code View on Trac
Changelog Changelog
Version | Description |
---|---|
3.0.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
The returned array of
[
'term_id'
=> ID,
'term_taxonomy_id'
=> ID]
returns a string for the ID instead of ints. If you need int values you’ll need to convert them to ints.
Checks to see if ‘Uncategorized’ category exists
Check if the ‘Uncategorized’ category exists
Note:
term_exists()
runs a database query.get_term()
can be used for the same purpose, except it uses the term cache.$term
= term_exists(
'Uncategorized'
,
'category'
);
if
(
$term
!== 0 &&
$term
!== null ) {
echo
__(
"'Uncategorized' category exists!"
,
"textdomain"
);
}
Checks to see if ‘Untagged’ post_tag category exists
Note:
term_exists()
runs a database query.get_term()
can be used for the same purpose, except it uses the term cache.$term
= term_exists(
'Untagged'
,
'post_tag'
);
if
(
$term
!== 0 &&
$term
!== null ) {
echo
__(
"'Untagged' post_tag exists!"
,
"textdomain"
);
}