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( $post->ID, 'foo', true )
:
$post->foo
It’s really that simple. Behind the scenes, the reference to $post->foo
is handled by WP_Post
‘s magic getter, which in turn calls 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 )
.
add_filter( 'get_post_metadata', 'add_dynamic_post_meta', 10, 4 ); /** * Add dynamically-generated "post meta" to `\WP_Post` objects * * This makes it possible to access dynamic data related to a post object by simply referencing `$post->foo`. * That keeps the calling code much cleaner than if it were to have to do something like * `$foo = some_custom_logic( get_post_meta( $post->ID, 'bar', true ) ); echo esc_html( $foo )`. * * @param mixed $value * @param int $post_id * @param string $meta_key * @param int $single @todo handle the case where this is false * * @return mixed * `null` to instruct `get_metadata()` to pull the value from the database * Any non-null value will be returned as if it were pulled from the database */ function add_dynamic_post_meta( $value, $post_id, $meta_key, $single ) { $post = get_post( $post_id ); if ( 'page' != $post->post_type ) { return $value; } switch ( $meta_key ) { case 'verbose_page_template': $value = "The page template is " . ( $post->_wp_page_template ?: 'not assigned' ); break; } return $value; }
Amazing feature. I used the same for user object but haven’t tried for posts.
That is absolutely marvelous. Genius in fact. Very good idea. Thank you for sharing. ^^
I’ve discovered this litterally by accident ! :D