Wordpress - Custom post meta field effect on the performance on the post

To answer this, I have gone and done some tests on this, and the results was actually mind blowing.

Here is my test

To this yourself, set yourself up with a test page. Just simply copy page.php, rename it and delete the loop. Now just create a new page in the back end. Before you start, first test your timer with empty info to get the amount of queries without any data

I have created 5 meta fields altogether for a test post,

  • enclosure,
  • First name,
  • Last name,
  • packages and
  • post_views_count

My test post had an ID of 530. Inside a post you can just simply use $post->ID or get_the_ID() to set the post ID

So my first test was as follows:

<?php               
       timer_start();       

       $a = get_post_meta(530, 'enclosure', true);
       $b = get_post_meta(530, 'First name', true);
       $c = get_post_meta(530, 'Last name', true);
       $d = get_post_meta(530, 'packages', true);
       $e = get_post_meta(530, 'post_views_count', true);
?>
<p><?php echo get_num_queries(); ?> queries in <?php timer_stop(1, 5); ?> seconds. </p>

which gave me the following results

1 queries in 0.00195 seconds.

My second test was as follows:

<?php               
       timer_start();       

       $a = get_post_meta(530);
?>
<p><?php echo get_num_queries(); ?> queries in <?php timer_stop(1, 5); ?> seconds. </p>

which, surprisingly gave the same result

1 queries in 0.00195 seconds.

If you look at the source code for get_post_meta(), you'll see that get_post_meta() is simply just a wrapper for get_metadata(). So this is were you need to look. The source code for get_metadata(), you'll see that the metadata get cached.

So on your question about which to use and about performance, the answer will be, it is up to you. You have seen the proof in the results

In my personal opinion, if you need to retrieve 10 meta data fields, (or in my case 5), use the second approach in my answer.

$a = get_post_meta(530);

It is not only quicker to write, but you should also not repeat code. Another point to note here, the second approach holds all meta fields in an array which can be very easily accessed and retrieved

Just as matter of example, here is my output from $a if I do a var_dump( $a );

array(9) {
  ["_edit_lock"]=>
  array(1) {
    [0]=>
    string(12) "1414838328:1"
  }
  ["_edit_last"]=>
  array(1) {
    [0]=>
    string(1) "1"
  }
  ["_custom_sidebar_per_page"]=>
  array(1) {
    [0]=>
    string(7) "default"
  }
  ["post_views_count"]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
  ["packages"]=>
  array(1) {
    [0]=>
    string(1) "0"
  }
  ["repeatable_names"]=>
  array(1) {
    [0]=>
    string(79) "a:1:{i:0;a:3:{s:4:"role";s:4:"fool";s:4:"name";s:6:"Pieter";s:3:"url";s:0:"";}}"
  }
  ["enclosure"]=>
  array(1) {
    [0]=>
    string(105) "http://localhost/wordpress/wp-content/uploads/2014/09/Nissan-Navara-Tough-City.avi
13218974
video/avi
"
  }
  ["First name"]=>
  array(1) {
    [0]=>
    string(3) "Tom"
  }
  ["Last name"]=>
  array(1) {
    [0]=>
    string(5) "Storm"
  }
}

You can now access any of the returned meta data in your post as follows:

echo $a['First name'][0] . " " . $a['Last name'][0] . "<br>";

Which will display

Tom Storm