In this article I want to show you a alternative, lesser known, way to access post meta values other than using the get_post_meta() function.
$post and $posts
As you may know most pages in WordPress get auto populated with $post and $posts variables. Where $post represents the post object for the current page. And $posts will be an array of all post objects for that page.
And you would get an output similar to the output shown below.
Note that the output is not an array but an instance of the WP_Post class, and that it holds all standard values associated with a post. We’ll get back to this later.
If you want to access a post meta value in your code you would normally use the get_post_meta() function.
To test this we first have to add a meta value to a post. In the code below we use the add_post_meta() function to add a some_meta_field meta field and give it a value of some_value.
We can than use the get_post_meta() function to access the some_meta_field on the post.
This would off course output ‘some_value‘.
Access meta fields through WP_Post
As I mentioned earlier the $post variable is not a standard array, but an instance of the WP_Post class. When we used print_r() to check the contents of the $post variable we saw that it only contains the values that are standard for every post object. It doesn’t show any meta values. Of course it couldn’t because meta fields can be different for every post, so they can’t be coded into the class.
But in fact the WP_Post class can access the meta fields. If we would check out the code for the class we can see that WP_Post implements the magic __get() method.
The function of the __get() method is to provide a fallback to when a property is called on a class that doesn’t exist. So when we try to access $post->i_dont_exist the __get() method is invoked to try and resolve a value for that property.
Below is the actual implementation of the __get() method on the WP_Post class in wp-includes/class-wp-post.php.
On line 47 of the gist above you can see that if all else fails the __get() method will try to get a post meta field value for the non existing property.
This means that if we try to access a meta field on the $post object like so: $post->some_meta the __get() method gets invoked and it will use the get_post_meta function to resolve the value.
To get back to the some_meta_field example earlier in this post. We could omit the get_post_meta() function and just access the some_meta_field on the $post object like shown below.
This will return ‘some_value’ just like the get_post_meta() function did but as you can see this looks a lot cleaner.
If you look at the __get() method on WP_Post class you will notice some more values that can be accessed directly. The Gist below shows the values that can be resolved.
Accessing post meta fields through the WP_Post object could clean up your code a quite bit. But there may be cases where this is not the best approach so, as always, use it wisely.
If you would like to comment on this post please do so under the copy of this article on Dev.to.
Thanks for reading.