metadata_exists

Determine if a meta key is set for a given object.

Description

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

Parameters (3)

0. $meta_type (string)
Type of object metadata is for (e.g., comment, post, or user)
1. $object_id (int)
ID of the object metadata is for
2. $meta_key (string)
The meta key.

Usage

  1. if ( !function_exists( 'metadata_exists' ) ) { 
  2. require_once ABSPATH . WPINC . '/meta.php'; 
  3.  
  4. // Type of object metadata is for (e.g., comment, post, or user) 
  5. $meta_type = ''; 
  6.  
  7. // ID of the object metadata is for 
  8. $object_id = -1; 
  9.  
  10. // The meta key. 
  11. $meta_key = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = metadata_exists($meta_type, $object_id, $meta_key); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/meta.php  
  1. function metadata_exists( $meta_type, $object_id, $meta_key ) { 
  2. if ( ! $meta_type || ! is_numeric( $object_id ) ) { 
  3. return false; 
  4.  
  5. $object_id = absint( $object_id ); 
  6. if ( ! $object_id ) { 
  7. return false; 
  8.  
  9. /** This filter is documented in wp-includes/meta.php */ 
  10. $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, true ); 
  11. if ( null !== $check ) 
  12. return (bool) $check; 
  13.  
  14. $meta_cache = wp_cache_get( $object_id, $meta_type . '_meta' ); 
  15.  
  16. if ( !$meta_cache ) { 
  17. $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 
  18. $meta_cache = $meta_cache[$object_id]; 
  19.  
  20. if ( isset( $meta_cache[ $meta_key ] ) ) 
  21. return true; 
  22.  
  23. return false;