Accessing Post Meta and More Via $post->meta_key


This is my new answer to, “What’s your favorite relatively unknown part of Core?” (Previously, it was the declaration of human rights embedded into has_cap().) It turns out there’s a much better way to fetch post meta than get_post_meta():
It’s really that simple. Behind the scenes, get_metadata() will call get_post_meta( $post->ID, ‘foo’, true ), making your code so much cleaner and more readable.
This was introduced 4 years ago in WordPress 3.5, but I just now stumbled upon it. Why has nobody ever told me this?
You can even extend it to introduce dynamically-generated fields, so you can call echo esc_html( $post->bar ) instead of $bar = some_custom_logic( get_post_meta( $post->ID, ‘bar’, true ) ); echo esc_html( $bar ).
Source: https://managewp.org/articles/13752/accessing-post-meta-and-more-via-post-meta-key




source https://williechiu40.wordpress.com/2016/11/02/accessing-post-meta-and-more-via-post-meta_key/