term_exists() WP 3.0.0
Check if a given taxonomy element exists. Returns either term ID or a term array if the element (term) exists.
1 time = 0.000419s = fast | 50000 times = 14.74s = slow | PHP 7.0.14, WP 4.7
No Hooks.
Return
null/int/array
-
Taxonomy element ID (term id), if the taxonomy is not specified but the term ID exists.
-
Array if taxonomy is specified and term is found:
[ 'term_id'=> 'term id', 'term_taxonomy_id'=> 'taxonomy id' ]
- 0/false - if the term doesn't exist.
Usage
term_exists( $term, $taxonomy, $parent );
- $term(int/string) (required)
- The term to check. Accepts term ID, slug, or name.
- $taxonomy(string)
- The taxonomy name to use.
Default: '' - $parent(int)
- ID of parent term under which to confine the exists search.
Default: null
Examples
#1 Usage examples
$term = term_exists( 'miscellaneous' ); // Returns term ID with 'miscellaneous' slug $term = term_exists( 'Men Clothes' ); // Returns term ID with 'Men Clothes' name
#1.1 With a taxonomy
$term = term_exists( 'Men Clothes', 'my_tax' ); // Returns array // [term_id] => 80 // [term_taxonomy_id] => 84 // Term ID echo $term['term_id']; // Taxonomy ID echo $term['term_taxonomy_id'];
Notes
- Global. wpdb. $wpdb WordPress database abstraction object.
Changelog
Since 3.0.0 | Introduced. |
Code of term_exists() term exists WP 5.6
Related Functions
From tag: Conditional tags (all)
- cat_is_ancestor_of()
- comments_open()
- email_exists()
- has_category()
- has_custom_header()
- has_excerpt()
- has_nav_menu()
- has_post_thumbnail()
- has_shortcode()
- has_tag()
- has_term()
- have_comments()
- have_posts()
- in_category()
- in_the_loop()
- is_404()
- is_active_sidebar()
- is_admin()
- is_admin_bar_showing()
- is_archive()
- is_attachment()
- is_author()
- is_blog_admin()
- is_blog_installed()
- is_category()
- is_child_theme()
- is_comment_feed()
- is_customize_preview()
- is_date()
- is_day()
- is_dynamic_sidebar()
- is_embed()
- is_feed()
- is_front_page()
- is_header_video_active()
- is_home()
- is_local_attachment()
- is_main_query()
- is_month()
- is_multi_author()
- is_multisite()
- is_nav_menu()
- is_network_admin()
- is_new_day()
- is_page()
- is_page_template()
- is_paged()
- is_plugin_active()
- is_post_type_archive()
- is_post_type_hierarchical()
- is_preview()
- is_robots()
- is_search()
- is_single()
- is_singular()
- is_ssl()
- is_sticky()
- is_tag()
- is_tax()
- is_taxonomy_hierarchical()
- is_textdomain_loaded()
- is_time()
- is_trackback()
- is_user_admin()
- is_user_logged_in()
- is_year()
- pings_open()
- post_exists()
- post_password_required()
- shortcode_exists()
- taxonomy_exists()
- term_is_ancestor_of()
- wp_attachment_is()
- wp_attachment_is_image()
- wp_doing_ajax()
- wp_doing_cron()
- wp_is_mobile()
- wp_is_post_autosave()
- wp_is_post_revision()
- wp_script_is()
More from category: Any taxonomy
- edit_term_link()
- get_edit_term_link()
- get_taxonomies()
- get_taxonomy()
- get_term()
- get_term_by()
- get_term_children()
- get_term_field()