Advertisement
sayful

WP post_meta Function Examples

Mar 9th, 2014
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1.  <?php
  2. /*******************
  3. This function handles the mood and listening_to meta tags.
  4. It can be called with an action of update, delete, and get (default)
  5. When called with an action of update, either $mood or $listening_to must be provided.
  6. i.e. mood_music( $post->ID, 'update', 'Happy', 'Bon Jovi - It's My Life' );
  7. *******************/
  8. function mood_music( $post_id, $action = 'get', $mood = 0, $listening_to = 0 ) {
  9.  
  10.   //Let's make a switch to handle the three cases of 'Action'
  11.   switch ($action) {
  12.     case 'update' :
  13.       if( ! $mood && ! $listening_to )
  14.         //If nothing is given to update, end here
  15.         return false;
  16.      
  17.       //add_post_meta usage:
  18.       //add_post_meta( $post_id, $meta_key, $meta_value, $unique = false )
  19.      
  20.       //If the $mood variable is supplied,
  21.       //add a new key named 'mood', containing that value.
  22.       //If the 'mood' key already exists on this post,
  23.       //this command will simply add another one.
  24.       if( $mood ) {
  25.         add_post_meta( $post_id, 'mood', $mood );
  26.         return true;
  27.         }
  28.       //update_post_meta usage:
  29.       //update_post_meta( $post_id, $meta_key, $meta_value )
  30.      
  31.       //If the $listening_to variable is supplied,
  32.       //add a new key named 'listening_to', containing that value.
  33.       //If the 'listening_to' key already exists on this post,
  34.       //this command will update it to the new value
  35.       if( $listening_to ) {
  36.         add_post_meta( $post_id, 'listening_to', $listening_to, true ) or
  37.           update_post_meta( $post_id, 'listening_to', $listening_to );
  38.         return true;
  39.       }
  40.     case 'delete' :
  41.       //delete_post_meta usage:
  42.       //delete_post_meta( $post_id, $meta_key, $prev_value = ' ' )
  43.    
  44.       //This will delete all instances of the following keys from the given post
  45.       delete_post_meta( $post_id, 'mood' );
  46.       delete_post_meta( $post_id, 'listening_to' );
  47.      
  48.       //To only delete 'mood' if it's value is 'sad':
  49.       //delete_post_meta( $post_id, 'mood', 'sad' );
  50.     break;
  51.     case 'get' :
  52.       //get_post_custom usage:
  53.       //get_post_meta( $post_id, $meta_key, $single value = false )
  54.  
  55.       //$stored_moods will be an array containing all values of the meta key 'mood'
  56.       $stored_moods = get_post_meta( $post_id, 'mood' );
  57.       //$stored_listening_to will be the first value of the key 'listening_to'
  58.       $stored_listening_to = get_post_meta( $post_id, 'listening_to', 'true' );
  59.  
  60.       //Now we need a nice ouput format, so that
  61.       //the user can implement it how he/she wants:
  62.       //ie. echo mood_music( $post->ID, 'get' );
  63.      
  64.       $return = '<div class='mood-music'>';
  65.       if ( ! empty( $stored_moods ) )
  66.         $return .= '<strong>Current Mood</strong>: ';
  67.       foreach( $stored_moods as $mood )
  68.         $return .= $mood . ', ';
  69.       $return .= '<br/>';
  70.  
  71.       if ( ! empty( $stored_listening_to ) ) {
  72.         $return .= '<strong>Currently Listening To</strong>: ';
  73.         $return .= $stored_listening_to;
  74.         }
  75.       $return .= '</div>';
  76.      
  77.       return $return;
  78.     default :
  79.       return false;
  80.     break;
  81.   } //end switch
  82. } //end function
  83. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement