get_metadata_by_mid

Get meta data by meta ID.

Description

(object|false) get_metadata_by_mid( (string) $meta_type, (int) $meta_id ); 

Returns (object|false)

Meta object or false.

Parameters (2)

0. $meta_type (string)
Type of object metadata is for (e.g., comment, post, term, or user).
1. $meta_id (int)
ID for a specific meta row

Usage

  1. if ( !function_exists( 'get_metadata_by_mid' ) ) { 
  2. require_once ABSPATH . WPINC . '/meta.php'; 
  3.  
  4. // Type of object metadata is for (e.g., comment, post, term, or user). 
  5. $meta_type = ''; 
  6.  
  7. // ID for a specific meta row 
  8. $meta_id = -1; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_metadata_by_mid($meta_type, $meta_id); 
  12.  

Defined (1)

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

/wp-includes/meta.php  
  1. function get_metadata_by_mid( $meta_type, $meta_id ) { 
  2. global $wpdb; 
  3.  
  4. if ( ! $meta_type || ! is_numeric( $meta_id ) || floor( $meta_id ) != $meta_id ) { 
  5. return false; 
  6.  
  7. $meta_id = intval( $meta_id ); 
  8. if ( $meta_id <= 0 ) { 
  9. return false; 
  10.  
  11. $table = _get_meta_table( $meta_type ); 
  12. if ( ! $table ) { 
  13. return false; 
  14.  
  15. $id_column = ( 'user' == $meta_type ) ? 'umeta_id' : 'meta_id'; 
  16.  
  17. $meta = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM $table WHERE $id_column = %d", $meta_id ) ); 
  18.  
  19. if ( empty( $meta ) ) 
  20. return false; 
  21.  
  22. if ( isset( $meta->meta_value ) ) 
  23. $meta->meta_value = maybe_unserialize( $meta->meta_value ); 
  24.  
  25. return $meta;