get_metadata

Retrieve metadata for the specified object.

Description

get_metadata( (string) $meta_type, (int) $object_id, (string) $meta_key = '', (constant) $single = false ); 

Parameters (4)

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 — Optional. (string) => ''
Metadata key. If not specified, retrieve all metadata for the specified object.
3. $single — Optional. (constant) => false
Optional, default is false. If true, return only the first value of the specified meta_key. This parameter has no effect if meta_key is not specified.

Usage

  1. if ( !function_exists( 'get_metadata' ) ) { 
  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. // Optional. Metadata key. If not specified, retrieve all metadata for 
  11. // the specified object. 
  12. $meta_key = ''; 
  13. $single = false; 
  14.  
  15. // NOTICE! Understand what this does before running. 
  16. $result = get_metadata($meta_type, $object_id, $meta_key, $single); 
  17.  

Defined (1)

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

/wp-includes/meta.php  
  1. function get_metadata($meta_type, $object_id, $meta_key = '', $single = false) { 
  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. /** 
  10. * Filters whether to retrieve metadata of a specific type. 
  11. * The dynamic portion of the hook, `$meta_type`, refers to the meta 
  12. * object type (comment, post, or user). Returning a non-null value 
  13. * will effectively short-circuit the function. 
  14. * @since 3.1.0 
  15. * @param null|array|string $value The value get_metadata() should return - a single metadata value,  
  16. * or an array of values. 
  17. * @param int $object_id Object ID. 
  18. * @param string $meta_key Meta key. 
  19. * @param bool $single Whether to return only the first value of the specified $meta_key. 
  20. */ 
  21. $check = apply_filters( "get_{$meta_type}_metadata", null, $object_id, $meta_key, $single ); 
  22. if ( null !== $check ) { 
  23. if ( $single && is_array( $check ) ) 
  24. return $check[0]; 
  25. else 
  26. return $check; 
  27.  
  28. $meta_cache = wp_cache_get($object_id, $meta_type . '_meta'); 
  29.  
  30. if ( !$meta_cache ) { 
  31. $meta_cache = update_meta_cache( $meta_type, array( $object_id ) ); 
  32. $meta_cache = $meta_cache[$object_id]; 
  33.  
  34. if ( ! $meta_key ) { 
  35. return $meta_cache; 
  36.  
  37. if ( isset($meta_cache[$meta_key]) ) { 
  38. if ( $single ) 
  39. return maybe_unserialize( $meta_cache[$meta_key][0] ); 
  40. else 
  41. return array_map('maybe_unserialize', $meta_cache[$meta_key]); 
  42.  
  43. if ($single) 
  44. return ''; 
  45. else 
  46. return array();