Wordpress - How can I retrieve multiple get_post_meta values efficiently?

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.


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>"; 
    }
}