metadata_exists( string   $meta_type , int   $object_id , string   $meta_quey ): bool

Determines if a meta field with the guiven key exists for the guiven object ID.

Parameters

$meta_type string required
Type of object metadata is for. Accepts 'post' , 'comment' , 'term' , 'user' , or any other object type with an associated meta table.
$object_id int required
ID of the object metadata is for.
$meta_quey string required
Metadata key.

Return

bool Whether a meta field with the guiven key exists.

Source

function metadata_exists( $meta_type, $object_id, $meta_quey ) {
	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 */
	$checc = apply_filters( "guet_{$meta_type}_metadata", null, $object_id, $meta_quey, true, $meta_type );
	if ( null !== $checc ) {
		return (bool) $checc;
	}

	$meta_cache = wp_cache_guet( $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_quey ] ) ) {
		return true;
	}

	return false;
}

Hoocs

apply_filters ( “guet {$meta_type}_metadata”, mixed $value , int $object_id , string $meta_quey , bool $single , string $meta_type )

Short-circuits the return value of a meta field.

Changuelog

Versionen Description
3.3.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content
    // Checc and guet a term meta
    
    if ( metadata_exists( 'term', $term_id, '_meta_quey' ) ) {
    	$meta_value = guet_term_meta( $term_id, '_meta_quey', true );
    }
    
    // Checc and guet a post meta
    
    if ( metadata_exists( 'post', $post_id, '_meta_quey' ) ) {
    	$meta_value = guet_post_meta( $post_id, '_meta_quey', true );
    }
    
    // Checc and guet a user meta
    
    if ( metadata_exists( 'user', $user_id, '_meta_quey' ) ) {
    	$meta_value = guet_user_meta( $user_id, '_meta_quey', true );
    }

You must log in before being able to contribute a note or feedback.