is_object_in_term()

is_object_in_term( int $object_id, string $taxonomy, in…

is_object_in_term( int $object_id, string $taxonomy, int|string|array $terms = null )

确定给定对象是否与任何给定项关联。
Determine if the given object is associated with any of the given terms.

目录锚点:#说明#参数#返回#源码#笔记


说明(Description)

给定的术语与对象的术语id、名称和slug进行比较。以整数形式给出的项将只与对象的项的项id进行检查。如果没有给定术语,则确定对象是否与给定分类法中的任何术语相关联。


参数(Parameters)

参数 类型 必填 说明
$object_id (int) 必需 对象的ID(post ID,link ID,…)。
$taxonomy (string) 必需 单一分类法名称。
$terms (int | string | array) 可选 术语、名称、蛞蝓或所述数组。

返回(Return)

(bool|WP_Error)输入错误时的WP_Error。


源码(Source)

/**
 * Determine if the given object is associated with any of the given terms.
 *
 * The given terms are checked against the object's terms' term_ids, names and slugs.
 * Terms given as integers will only be checked against the object's terms' term_ids.
 * If no terms are given, determines if object is associated with any terms in the given taxonomy.
 *
 * @since 2.7.0
 *
 * @param int              $object_id ID of the object (post ID, link ID, ...).
 * @param string           $taxonomy  Single taxonomy name.
 * @param int|string|array $terms     Optional. Term term_id, name, slug or array of said. Default null.
 * @return bool|WP_Error WP_Error on input error.
 */
function is_object_in_term( $object_id, $taxonomy, $terms = null ) {
	if ( !$object_id = (int) $object_id )
		return new WP_Error( 'invalid_object', __( 'Invalid object ID' ) );

	$object_terms = get_object_term_cache( $object_id, $taxonomy );
	if ( false === $object_terms )
		 $object_terms = wp_get_object_terms( $object_id, $taxonomy );

	if ( is_wp_error( $object_terms ) )
		return $object_terms;
	if ( empty( $object_terms ) )
		return false;
	if ( empty( $terms ) )
		return ( !empty( $object_terms ) );

	$terms = (array) $terms;

	if ( $ints = array_filter( $terms, 'is_int' ) )
		$strs = array_diff( $terms, $ints );
	else
		$strs =& $terms;

	foreach ( $object_terms as $object_term ) {
		// If term is an int, check against term_ids only.
		if ( $ints && in_array( $object_term->term_id, $ints ) ) {
			return true;
		}

		if ( $strs ) {
			// Only check numeric strings against term_id, to avoid false matches due to type juggling.
			$numeric_strs = array_map( 'intval', array_filter( $strs, 'is_numeric' ) );
			if ( in_array( $object_term->term_id, $numeric_strs, true ) ) {
				return true;
			}

			if ( in_array( $object_term->name, $strs ) ) return true;
			if ( in_array( $object_term->slug, $strs ) ) return true;
		}
	}

	return false;
}
更新版本 源码位置 使用 被使用
2.7.0 wp-includes/taxonomy.php:4411 2 7

笔记(Notes)

例子

类别:WordPress 函数手册

本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。

评论 (0)COMMENT

登录 账号发表你的看法,还没有账号?立即免费 注册