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?

  • 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. – Adam 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.." – Adam Apr 23 '12 at 20:22

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':
    [0] => Array(
| improve this answer | |

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();
        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 ) {
        return $custom_fields;

Then within your theme files you can do the following;

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

Source HERE

| improve this answer | |

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

| 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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