bb_update_meta

Adds and updates meta data in the database.

Description

bb_update_meta( $object_id, $meta_key, $meta_value, $type, (bool) $global = false ); 

Parameters (5)

0. $object_id
The object id.
1. $meta_key
The meta key.
2. $meta_value
The meta value.
3. $type
The type.
4. $global — Optional. (bool) => false
The global.

Usage

  1. if ( !function_exists( 'bb_update_meta' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'buddypress/bp-forums/bbpress/bb-includes/functions.bb-meta.php'; 
  3.  
  4. // The object id. 
  5. $object_id = null; 
  6.  
  7. // The meta key. 
  8. $meta_key = null; 
  9.  
  10. // The meta value. 
  11. $meta_value = null; 
  12.  
  13. // The type. 
  14. $type = null; 
  15.  
  16. // The global. 
  17. $global = false; 
  18.  
  19. // NOTICE! Understand what this does before running. 
  20. $result = bb_update_meta($object_id, $meta_key, $meta_value, $type, $global); 
  21.  

Defined (1)

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

/bp-forums/bbpress/bb-includes/functions.bb-meta.php  
  1. function bb_update_meta( $object_id = 0, $meta_key, $meta_value, $type, $global = false ) 
  2. global $bbdb; 
  3. if ( !is_numeric( $object_id ) || empty( $object_id ) && !$global ) { 
  4. return false; 
  5. $cache_object_id = $object_id = (int) $object_id; 
  6. switch ( $type ) { 
  7. case 'option': 
  8. $object_type = 'bb_option'; 
  9. break; 
  10. case 'user' : 
  11. global $wp_users_object; 
  12. $id = $object_id; 
  13. $return = $wp_users_object->update_meta( compact( 'id', 'meta_key', 'meta_value' ) ); 
  14. if ( is_wp_error( $return ) ) { 
  15. return false; 
  16. return $return; 
  17. break; 
  18. case 'forum' : 
  19. $object_type = 'bb_forum'; 
  20. break; 
  21. case 'topic' : 
  22. $object_type = 'bb_topic'; 
  23. break; 
  24. case 'post' : 
  25. $object_type = 'bb_post'; 
  26. break; 
  27. default : 
  28. $object_type = $type; 
  29. break; 
  30.  
  31. $meta_key = bb_sanitize_meta_key( $meta_key ); 
  32.  
  33. $meta_tuple = compact( 'object_type', 'object_id', 'meta_key', 'meta_value', 'type' ); 
  34. $meta_tuple = apply_filters( 'bb_update_meta', $meta_tuple ); 
  35. extract( $meta_tuple, EXTR_OVERWRITE ); 
  36.  
  37. $meta_value = $_meta_value = maybe_serialize( $meta_value ); 
  38. $meta_value = maybe_unserialize( $meta_value ); 
  39.  
  40. $cur = $bbdb->get_row( $bbdb->prepare( "SELECT * FROM `$bbdb->meta` WHERE `object_type` = %s AND `object_id` = %d AND `meta_key` = %s", $object_type, $object_id, $meta_key ) ); 
  41. if ( !$cur ) { 
  42. $bbdb->query( $bbdb->prepare( 
  43. "INSERT INTO `$bbdb->meta` ( `object_type`, `object_id`, `meta_key`, `meta_value` ) VALUES( %s, %d, %s, %s ) 
  44. ON DUPLICATE KEY UPDATE `meta_value` = VALUES( `meta_value` )",  
  45. $object_type, $object_id, $meta_key, $_meta_value 
  46. ) ); 
  47. } elseif ( $cur->meta_value != $meta_value ) { 
  48. $bbdb->update( $bbdb->meta, array( 'meta_value' => $_meta_value), array( 'object_type' => $object_type, 'object_id' => $object_id, 'meta_key' => $meta_key ) ); 
  49.  
  50. if ( $object_type === 'bb_option' ) { 
  51. $cache_object_id = $meta_key; 
  52. wp_cache_delete( $cache_object_id, 'bb_option_not_set' ); 
  53. wp_cache_delete( $cache_object_id, $object_type ); 
  54.  
  55. if ( !$cur ) { 
  56. return true;