返回介绍

metadata_exists()

发布于 2017-09-11 01:55:42 字数 3138 浏览 918 评论 0 收藏 0

metadata_exists( string $meta_type,  int $object_id,  string $meta_key )

Determine if a meta key is set for a given object


description


参数

$meta_type

(string) (Required) Type of object metadata is for (e.g., comment, post, or user)

$object_id

(int) (Required) ID of the object metadata is for

$meta_key

(string) (Required) Metadata key.


返回值

(bool) True of the key is set, false if not.


源代码

File: wp-includes/meta.php

function metadata_exists( $meta_type, $object_id, $meta_key ) {
	if ( ! $meta_type || ! is_numeric( $object_id ) ) {
		return false;
	}

	$object_id = absint( $object_id );
	if ( ! $object_id ) {
		return false;
	}

	/** This filter is documented in wp-includes/meta.php */
	$check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true );
	if ( null !== $check )
		return (bool) $check;

	$meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' );

	if ( !$meta_cache ) {
		$meta_cache = update_meta_cache( $meta_type, array( $object_id ) );
		$meta_cache = $meta_cache[$object_id];
	}

	if ( isset( $meta_cache[ $meta_key ] ) )
		return true;

	return false;
}

更新日志

Versiondescription
3.3.0Introduced.

相关函数

Uses

  • wp-includes/cache.php: wp_cache_get()
  • wp-includes/functions.php: absint()
  • wp-includes/plugin.php: apply_filters()
  • wp-includes/meta.php: update_meta_cache()
  • wp-includes/meta.php: get_{$meta_type}_metadata

Used By

  • wp-admin/includes/upgrade.php: wp_install_defaults()
  • wp-includes/class-wp-user.php: WP_User::__isset()
  • wp-includes/class-wp-post.php: WP_Post::__isset()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by vutuan.sw
    
    // Check and get a term meta
    
    if ( metadata_exists( 'term', $term_id, '_meta_key' ) ) {
    	$meta_value = get_term_meta( $term_id, '_meta_key', true );
    }
    
    // Check and get a post meta
    
    if ( metadata_exists( 'post', $post_id, '_meta_key' ) ) {
    	$meta_value = get_post_meta( $post_id, '_meta_key', true );
    }
    
    // Check and get a user meta
    
    if ( metadata_exists( 'user', $user_id, '_meta_key' ) ) {
    	$meta_value = get_user_meta( $user_id, '_meta_key', true );
    }
    

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

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

发布评论

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