If someone opens any post at the front-end, does it matter how many times I call get_post_meta
function? I thought it could increase performance if I won’t call that function 2-3 times instead of 20 calls on single post page.
2 Answers
One call to get_post_meta()
fetches all met keys and values for that post. All these values are then stored in the cache. The next call will just fetch the data from the cache. So you can safely call that function multiple times.
In details:
get_post_meta()
callsget_metadata('post', $post_id, $key, $single);
get_metadata()
checks the cache and callsupdate_meta_cache()
if it doesn’t find an existing cache.-
update_meta_cache()
fetches all entries with:"SELECT $column, meta_key, meta_value FROM $table WHERE $column IN ($id_list) ORDER BY $id_column ASC"
The same is true for user meta values.