5

I am looking for a solution to returning multiple get_post_meta values...

Current I am using a meta box array as follows:

$meta_boxes[] = array(
'id' => 'setlist',
'title' => 'Setlist Information',
'pages' => array('post'),
'fields' => array(
    array(
        'name' => 'Setlist 1',                  // field name
        'desc' => 'Entry 1',                                    // field description, optional
        'id' => $prefix . 'setlist_1',              // field id, i.e. the meta key
        'type' => 'text',                   // text box
        'std' => '',                                            // default value, optional
        'style' => 'width: 100px',              // custom style for field, added in v3.1
    ),
            array(
        'name' => 'Setlist 2',                  
        'desc' => 'Entry 2',                                    
        'id' => $prefix . 'setlist_2',              
        'type' => 'text',                                       
        'std' => '',                                            
        'style' => 'width: 100px',              
    ),

and so on so forth (i.e. setlist_3,4,5,6,7,8....)

In my single.php I have:

<?php if ( get_post_meta( $post->ID, 'rw_setlist_1', true ) ) : echo "<li>"; echo get_post_meta( $post->ID, 'rw_setlist_1', true ); echo "</li>"; ?>
<?php endif; ?>
<?php if ( get_post_meta( $post->ID, 'rw_setlist_2', true ) ) : echo "<li>"; echo get_post_meta( $post->ID, 'rw_setlist_2', true ); echo "</li>"; ?>
 <?php endif; ?>
 <?php if ( get_post_meta( $post->ID, 'rw_setlist_3', true ) ) : echo "<li>"; echo get_post_meta( $post->ID, 'rw_setlist_3', true ); echo "</li>"; ?>
 <?php endif; ?>
 <?php if ( get_post_meta( $post->ID, 'rw_setlist_4', true ) ) : echo "<li>"; echo get_post_meta( $post->ID, 'rw_setlist_4', true ); echo "</li>"; ?>
 <?php endif; ?>
 <?php if ( get_post_meta( $post->ID, 'rw_setlist_5', true ) ) : echo "<li>"; echo get_post_meta( $post->ID, 'rw_setlist_5', true ); echo "</li>"; ?>

The set-list values can range from 2 to 30...

Call me crazy, but I feel as if this method causes unnecessary and lengthy load times, am I right? So how would I go about creating a more efficient script for this that would check all the values in the array in a "simpler way".

5

The way your code is setup is just wrong, you are making two database calls for each custom field and if you have between 2-30 fields like this then that means you make over 60 calls to the database which can be done with a single call using get_post_custom() for ex:

$custom_fields = get_post_custom($post->ID);
for ($i = 1; $i <= 30; $i++) { 
    if(isset($custom_fields["rw_setlist_$i"])){
        echo "<li>"; 
        echo $custom_fields["rw_setlist_$i"];
    echo "</li>"; 
    }
}
  • Worked perfectly. Just tweaked the second echo out to echo get_post_meta( $post->ID, "rw_setlist_$i", true ); (p.s. thank you for heads up on the accept rate, so that's what those check marks are for) – mmaximalist Jul 9 '11 at 22:05
  • 2
    The statement about # of DB calls is inaccurate... Unless explicitly disabled, WP_Query preloads meta for all found posts into the object cache in a single query. If meta has not been set in the cache when you call get_post_meta() it will also preload all meta for that specific post in a single query. So worst case we are looking at a single query per post but more than likely no additional queries are made. EDIT: just realized that this is a six year old post... – ssnepenthe Oct 10 '17 at 15:59
5

I believe get_post_meta() (or more accurately, the get_metadata() function it calls) retrieves all metadata for a post in one query and then caches it for subsequent calls. As for the verbosity of your code, if they don't need to be explicitly ordered you could just save them all under a single key and grab them as an array.

  • Thank you for your swift response, not sure how your interpreting the word explicitly. But the values are ordered. They will be formatted into a list, hence the <li> tags. Theoretically I just wanted to say... if rw_setlist_(# variable) exists, then print it and add the <li> tags. I will do some research into your advice, just thought I'd provide more details. – mmaximalist Jul 9 '11 at 9:31
  • It may work with one key if they appear in the order they're added (though I'm not positive that will consistently be the case). If they are separate keys you could at least use a loop to output them: for ($i = 1; $i <= 30; $i++) { if(get_post_meta( $post->ID, 'rw_setlist_'.$i, true )) { etc.. – Milo Jul 9 '11 at 9:43
  • Thank you Milo. You and Bainternet both sent me in the right direction. – mmaximalist Jul 9 '11 at 22:08

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.