I have 27 items of post_meta data that I need to get out in a class that I use through-out my website.

Currently I'm using:

$this->postMetaData = get_metadata("post", $postID);

This returns an array of arrays... such as...

[first_element] => Array(
    [0] => firstValue
[second_element] => Array(
    [0] => secondValue

I then have to 'loop' through the array and 'flatten' it down so it becomes:

[first_element] => firstValue
[second_element] => secondValue

I then assign the array elements to variables in the class such as:

$this->firstElement = $myArray["first_element"];

As part of some micro-optimisation I was looking at I wondered if there was a better way of doing this. I've read about get_metadata and get_post_meta and wondered whether I'm better using get_post_meta (or get_metadata) as singles... for instance:

$this->firstElement = get_post_meta($postID, "first_element", true);

Or whether I should just use a bespoke query to get the items out of the database?

Any idea which is better?

Thanks :-)

  • 1
    This question comes down to 6 of one and half dozen of the other one ;-) – Pieter Goosen Jun 9 '15 at 12:02
  • My pleasure ;-) Enjoy – Pieter Goosen Jun 9 '15 at 12:09

Browse other questions tagged or ask your own question.