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 usually use two steps to retreive a list of posts and the associated metadata: call get_posts(or a custom WP_Query), and then loop through each post returned and retrieve each posts metadata one by one.

Is there any way to combine this into a single query? I guess I'm looking for something like a get_posts_with_meta() function?

share|improve this question
1  
Please clarify. Single SQL query? That would probably be convoluted and not very practical. Single PHP function? That would be trivial to write, but what difference would it make in practice? –  Rarst Apr 23 '12 at 19:07
    
What are you actually trying to accomplish? Without some context, it is difficult to understand the purpose/need for what you're asking. –  Chip Bennett Apr 23 '12 at 19:19
    
@ChipBennett He wants to return posts within the loop along with all of the associated post_meta (i.e. Meta Key/Meta Values) without necessarily having to define the key/value within the query or know what the keys/values are. –  userabuser Apr 23 '12 at 19:36
    
@userabuser but you're basically restating his question. I'm trying to understand why the OP wants to do this. –  Chip Bennett Apr 23 '12 at 20:06
    
You opened your question with "What.." and not "Why.." –  userabuser Apr 23 '12 at 20:22
show 2 more comments

3 Answers 3

It's not a single step, but it's easy to write & handle in code: get_post_custom() gives you all attached meta data. But be aware: Everything comes as array, even when it´s a single value. So

// Ever entry comes like this.
// Example single value for 'Hair color':
Array(
    [0] => Array(
        'Brown'
    )
)
share|improve this answer
add comment

Here is a snippet of code source: WPSnipp.com

Place this in your functions.php file...

function get_post_meta_all($post_id){
    global $wpdb;
    $data   =   array();
    $wpdb->query("
        SELECT `meta_key`, `meta_value`
        FROM $wpdb->postmeta
        WHERE `post_id` = $post_id
    ");
    foreach($wpdb->last_result as $k => $v){
        $data[$v->meta_key] =   $v->meta_value;
    };
    return $data;
}

Or using get_post_custom() you can do this;

Place this in your functions.php file...

if ( !function_exists('base_get_all_custom_fields') ) {
    function base_get_all_custom_fields()
    {
        global $post;
        $custom_fields = get_post_custom($post->ID);
        $hidden_field = '_';
        foreach( $custom_fields as $key => $value ){
            if( !empty($value) ) {
                $pos = strpos($key, $hidden_field);
                if( $pos !== false && $pos == 0 ) {
                    unset($custom_fields[$key]);
                }
            }
        }
        return $custom_fields;
    }
}

Then within your theme files you can do the following;

$custom_fields = base_get_all_custom_fields();
if( !empty($custom_fields) ) {
    print_r($custom_fields);
}

Source HERE

share|improve this answer
add comment
up vote -2 down vote accepted

Not worth it as it would be a convoluted query and work against most of the wp caching solutions.

share|improve this answer
2  
This is not right nor an appropriate answer. –  Wyck Apr 26 '12 at 23:22
    
How do you figure? –  flashape May 3 '12 at 22:43
add comment

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.