[wp-hackers] multiple calls to get_post_meta back to back as opposed to one call

Andrew Nacin wp at andrewnacin.com
Thu Feb 7 21:43:10 UTC 2013

On Thu, Feb 7, 2013 at 4:30 PM, Haluk Karamete <halukkaramete at gmail.com>wrote:

> This question can be formulated as
> Would it make sense ( from the mysql quiery point of view ) to build a
> functionality so that the following 2 liner code
> $fields = array('field_1','field_2',....'field_10');
> extract(get_post_metas($post->ID,$fields,true));
> will practically be speaking equal to something like
> $field_1 =  get_post_meta($post->ID,'field_1',true);
> $field_2 =  get_post_meta($post->ID,'field_2',true);
> ...
> $field_10 =  get_post_meta($post->ID,'field_10',true);
> First approach will be done with a single but slightly more
> complicated query whereas the latter will force 10 sql queries they
> tend to be simpler.

Test this and you will find that the latter is just one query, total. When
you call get_post_meta(), all meta for that post is queried and cached.

Additionally, only the first argument to get_post_meta() is required. Call
it with just a post ID and you'll get back all metadata.


More information about the wp-hackers mailing list