返回介绍

term_exists()

发布于 2017-09-11 10:30:50 字数 6364 浏览 1077 评论 0 收藏 0

term_exists( int|string $term,  string $taxonomy = '',  int $parent = null )

Check if Term exists.


description

Formerly is_term(), introduced in 2.3.0.


参数

$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


返回值

(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 the taxonomy is specified and the pairing exists.


源代码

File: wp-includes/taxonomy.php

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) );
}

更新日志

Versiondescription
3.0.0Introduced.

相关函数

Uses

  • wp-includes/formatting.php: wp_unslash()
  • wp-includes/formatting.php: sanitize_title()
  • wp-includes/wp-db.php: wpdb::get_row()
  • wp-includes/wp-db.php: wpdb::get_var()
  • wp-includes/wp-db.php: wpdb::prepare()

Used By

  • wp-admin/includes/class-wp-press-this.php: WP_Press_This::add_category()
  • wp-admin/includes/export.php: export_wp()
  • wp-admin/includes/taxonomy.php: category_exists()
  • wp-admin/includes/taxonomy.php: wp_insert_category()
  • wp-admin/includes/taxonomy.php: tag_exists()
  • wp-admin/includes/taxonomy.php: wp_create_term()
  • wp-includes/deprecated.php: is_term()
  • wp-includes/taxonomy.php: wp_unique_term_slug()
  • wp-includes/taxonomy.php: wp_update_term()
  • wp-includes/taxonomy.php: wp_insert_term()
  • wp-includes/taxonomy.php: wp_set_object_terms()
  • wp-includes/taxonomy.php: wp_remove_object_terms()
  • wp-includes/taxonomy.php: wp_delete_term()
  • Show 8 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 1You must log in to vote on the helpfulness of this note Contributed by Maje Media LLC

    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.

  2. 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" );
    }
    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文