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.
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.
get_metadata('post', $post_id, $key, $single);
get_metadata()checks the cache and calls
update_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.
...I thought it could increase performance if I won't call that function 2-3 times instead of 20 calls on single post page
Together with the excellent answer from @toscho, I will just add one additional thing, based on my latest digging.
If you don't set it otherwise WordPress queries will prefetch post meta values for all the posts the query returns.
This means even before you call
get_post_meta(); the meta values are already in the WordPress cache.
From this image, you may understand that typically WordPress prefetches the information for the metadata and also for the terms.
The conclusion is the same as from @toscho. You may call
get_post_meta(); whenever you need.
To get the basic details on how the caching looks, you also may check this post.