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 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".

share|improve this question
    
0% accept rate!! please go back to your old questions and accept some answers.its the least you can do to thank people who give you there time and answer your questions. –  Bainternet Jul 9 '11 at 10:26

2 Answers 2

up vote 0 down vote accepted

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>"; 
    }
}
share|improve this answer
    
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

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.

share|improve this answer
    
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

 
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.