Leave a Reply

2 comments

  1. $post->ID is what makes the meta value distinguished across all posts with the same meta key.

    So if you want to shorthand the get_post_meta call for the current post you can do this:

    function get_cuurent_post_meta($key){
        global $post;
        return get_post_meta($post->ID,$key,true);
    }
    

    and you can call it like this:

    echo get_cuurent_post_meta('custom_tags'.$userID);
    

    Now if its not the current post but any post you can do this:

    function get_meta_value_by_key($meta_key,$limit = 1){
        global $wpdb;
        if (1 == $limit)
            return $value = $wpdb->get_var( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT 1" , $meta_key) );
        else
            return $value = $wpdb->get_results( $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = %s LIMIT %d" , $meta_key,$limit) );
    }
    

    to get the first meta value of this key just call it like this:

    echo get_meta_value_by_key('custom_tags'.$userID);
    

    and to get all meta values of this key use the $limit param ex:

    $post_meta_array = get_meta_value_by_key('custom_tags'.$userID, 999);
    
  2. You can query your postmeta table directly using something like:

    global $wpdb;
    
    $metas = $wpdb->get_results( 
      $wpdb->prepare("SELECT meta_value FROM $wpdb->postmeta where meta_key = %s", 'add_your_key_here')
     );
    
    echo '<pre>';
    print_r( $metas );
    echo '</pre>';
    

    UPDATE
    After you get your $metas, you pass them in a loop to display the values:

    if( count($metas) > 0 ){ #check if we got any results
      foreach ($metas as $meta){
        echo $meta->meta_value . "<br />";
      }
    }