term_exists

函式
term_exists ( $term, $taxonomy = '', $parent = null )
引數
  • (int|string) $term The term to check. Accepts term ID, slug, or name.
    Required:
  • (string) $taxonomy Optional. The taxonomy name to use.
    Required:
    Default: (empty)
  • (int) $parent Optional. ID of parent term under which to confine the exists search.
    Required:
    Default: 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 if the taxonomy is specified and the pairing exists. Returns 0 if term ID 0 is passed to the function.
定義位置
相關方法
email_existsusername_existscategory_existstag_existspost_exists
引入
3.0.0
棄用
-

term_exists: 這個函式檢查一個術語是否存在於一個給定的分類法中。

確定一個分類學術語是否存在。

前身是is_term(),在2.3.0中引入。

關於這個函式和類似的主題函式的更多資訊,請檢視《主題開發者手冊》中的{@link Conditional Tags}文章。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function term_exists( $term, $taxonomy = '', $parent = null ) {
global $_wp_suspend_cache_invalidation;
if ( null === $term ) {
return null;
}
$defaults = array(
'get' => 'all',
'fields' => 'ids',
'number' => 1,
'update_term_meta_cache' => false,
'order' => 'ASC',
'orderby' => 'term_id',
'suppress_filter' => true,
);
// Ensure that while importing, queries are not cached.
if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
// @todo Disable caching once #52710 is merged.
$defaults['cache_domain'] = microtime();
}
if ( ! empty( $taxonomy ) ) {
$defaults['taxonomy'] = $taxonomy;
$defaults['fields'] = 'all';
}
/**
* Filters default query arguments for checking if a term exists.
*
* @since 6.0.0
*
* @param array $defaults An array of arguments passed to get_terms().
* @param int|string $term The term to check. Accepts term ID, slug, or name.
* @param string $taxonomy The taxonomy name to use. An empty string indicates
* the search is against all taxonomies.
* @param int|null $parent ID of parent term under which to confine the exists search.
* Null indicates the search is unconfined.
*/
$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent );
if ( is_int( $term ) ) {
if ( 0 === $term ) {
return 0;
}
$args = wp_parse_args( array( 'include' => array( $term ) ), $defaults );
$terms = get_terms( $args );
} else {
$term = trim( wp_unslash( $term ) );
if ( '' === $term ) {
return null;
}
if ( ! empty( $taxonomy ) && is_numeric( $parent ) ) {
$defaults['parent'] = (int) $parent;
}
$args = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );
$terms = get_terms( $args );
if ( empty( $terms ) || is_wp_error( $terms ) ) {
$args = wp_parse_args( array( 'name' => $term ), $defaults );
$terms = get_terms( $args );
}
}
if ( empty( $terms ) || is_wp_error( $terms ) ) {
return null;
}
$_term = array_shift( $terms );
if ( ! empty( $taxonomy ) ) {
return array(
'term_id' => (string) $_term->term_id,
'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
);
}
return (string) $_term;
}
function term_exists( $term, $taxonomy = '', $parent = null ) { global $_wp_suspend_cache_invalidation; if ( null === $term ) { return null; } $defaults = array( 'get' => 'all', 'fields' => 'ids', 'number' => 1, 'update_term_meta_cache' => false, 'order' => 'ASC', 'orderby' => 'term_id', 'suppress_filter' => true, ); // Ensure that while importing, queries are not cached. if ( ! empty( $_wp_suspend_cache_invalidation ) ) { // @todo Disable caching once #52710 is merged. $defaults['cache_domain'] = microtime(); } if ( ! empty( $taxonomy ) ) { $defaults['taxonomy'] = $taxonomy; $defaults['fields'] = 'all'; } /** * Filters default query arguments for checking if a term exists. * * @since 6.0.0 * * @param array $defaults An array of arguments passed to get_terms(). * @param int|string $term The term to check. Accepts term ID, slug, or name. * @param string $taxonomy The taxonomy name to use. An empty string indicates * the search is against all taxonomies. * @param int|null $parent ID of parent term under which to confine the exists search. * Null indicates the search is unconfined. */ $defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent ); if ( is_int( $term ) ) { if ( 0 === $term ) { return 0; } $args = wp_parse_args( array( 'include' => array( $term ) ), $defaults ); $terms = get_terms( $args ); } else { $term = trim( wp_unslash( $term ) ); if ( '' === $term ) { return null; } if ( ! empty( $taxonomy ) && is_numeric( $parent ) ) { $defaults['parent'] = (int) $parent; } $args = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults ); $terms = get_terms( $args ); if ( empty( $terms ) || is_wp_error( $terms ) ) { $args = wp_parse_args( array( 'name' => $term ), $defaults ); $terms = get_terms( $args ); } } if ( empty( $terms ) || is_wp_error( $terms ) ) { return null; } $_term = array_shift( $terms ); if ( ! empty( $taxonomy ) ) { return array( 'term_id' => (string) $_term->term_id, 'term_taxonomy_id' => (string) $_term->term_taxonomy_id, ); } return (string) $_term; }
function term_exists( $term, $taxonomy = '', $parent = null ) {
	global $_wp_suspend_cache_invalidation;

	if ( null === $term ) {
		return null;
	}

	$defaults = array(
		'get'                    => 'all',
		'fields'                 => 'ids',
		'number'                 => 1,
		'update_term_meta_cache' => false,
		'order'                  => 'ASC',
		'orderby'                => 'term_id',
		'suppress_filter'        => true,
	);

	// Ensure that while importing, queries are not cached.
	if ( ! empty( $_wp_suspend_cache_invalidation ) ) {
		// @todo Disable caching once #52710 is merged.
		$defaults['cache_domain'] = microtime();
	}

	if ( ! empty( $taxonomy ) ) {
		$defaults['taxonomy'] = $taxonomy;
		$defaults['fields']   = 'all';
	}

	/**
	 * Filters default query arguments for checking if a term exists.
	 *
	 * @since 6.0.0
	 *
	 * @param array      $defaults An array of arguments passed to get_terms().
	 * @param int|string $term     The term to check. Accepts term ID, slug, or name.
	 * @param string     $taxonomy The taxonomy name to use. An empty string indicates
	 *                             the search is against all taxonomies.
	 * @param int|null   $parent   ID of parent term under which to confine the exists search.
	 *                             Null indicates the search is unconfined.
	 */
	$defaults = apply_filters( 'term_exists_default_query_args', $defaults, $term, $taxonomy, $parent );

	if ( is_int( $term ) ) {
		if ( 0 === $term ) {
			return 0;
		}
		$args  = wp_parse_args( array( 'include' => array( $term ) ), $defaults );
		$terms = get_terms( $args );
	} else {
		$term = trim( wp_unslash( $term ) );
		if ( '' === $term ) {
			return null;
		}

		if ( ! empty( $taxonomy ) && is_numeric( $parent ) ) {
			$defaults['parent'] = (int) $parent;
		}

		$args  = wp_parse_args( array( 'slug' => sanitize_title( $term ) ), $defaults );
		$terms = get_terms( $args );
		if ( empty( $terms ) || is_wp_error( $terms ) ) {
			$args  = wp_parse_args( array( 'name' => $term ), $defaults );
			$terms = get_terms( $args );
		}
	}

	if ( empty( $terms ) || is_wp_error( $terms ) ) {
		return null;
	}

	$_term = array_shift( $terms );

	if ( ! empty( $taxonomy ) ) {
		return array(
			'term_id'          => (string) $_term->term_id,
			'term_taxonomy_id' => (string) $_term->term_taxonomy_id,
		);
	}

	return (string) $_term;
}

常見問題

FAQs
檢視更多 >