Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to get my all posts-meta, But on the base of key. For example the main query for getting the post-meta is

 <?php $meta_values = get_post_meta($post_id, $key, $single); ?> 

Now i want to get this post-meta on the base of key only. My current query to get the post-meta is

<?php echo get_post_meta($post->ID, 'custom_tags'.$userID, true); ?>

And i want to remove $post->ID from this query is there any way to get this. Thanks

share|improve this question

2 Answers 2

$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);
share|improve this answer
    
There is one thing i just have to ask is. when i echo the $post_meta_array it show Array. And when i use print_r($post_meta_array) it show me data something like this Array ( [0] => stdClass Object ( [meta_value] => $custom_tags ) [1] => stdClass Object ( [meta_value] => tag2,tag3,tag4 ) [2] => stdClass Object ( [meta_value] => garlic,soupe ) ) ;. How can i show them in one line with now objects. –  Adi Apr 8 '13 at 12:48
    
You can chage the output of [get_results ](codex.wordpress.org/Class_Reference/wpdb#SELECT_Generic_Results) and then when you have an array you can use implode –  Bainternet Apr 8 '13 at 13:02

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 />";
  }
}
share|improve this answer
    
The problem is same as i commet above. Here is my comment. There is one thing i just have to ask is. when i use print_r($metas) it show me data something like this Array ( [0] => stdClass Object ( [meta_value] => $custom_tags ) [1] => stdClass Object ( [meta_value] => tag2,tag3,tag4 ) [2] => stdClass Object ( [meta_value] => garlic,soupe ) ) ;. How can i show them in one line with now objects. –  Adi Apr 8 '13 at 13:02
    
Updated my answer. –  RRikesh Apr 8 '13 at 14:12
    
@RRikesh Please take a look at $wpdb->prepare(). Always prepare your statements, no matter what. –  kaiser Apr 9 '13 at 22:15
    
@kaiser Thanks, I updated my answer. BTW, should I prepare even if I'm hard-coding the value? –  RRikesh Apr 10 '13 at 5:09
    
Basically it's a sprintf() replacement that cares about replacing %s and %d. So I'd do it anyway. Even if it's just for other people reading my code or preparing it in case I later move to a dynamic var. To answer your question: No, not in a private case ;) –  kaiser Apr 10 '13 at 8:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.