WordPress: Access post meta fields through WP_Post.
Tags: wordpress, webdev, php, tutorial
Published: 2019-10-06 18:25:56

---
title: WordPress: Access post meta fields through WP_Post.
published: true
description: 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.
canonical_url: https://since1979.dev/wordpress-access-post-meta-fields-through-wp-post/
cover_image: https://since1979.dev/wp-content/uploads/2019/10/access-post-meta-fields-through-wp-post-1000x450.jpg
tags: wordpress, webdev, php, tutorial
---
[Originally posted on my website on October 6th 2019](https://since1979.dev/wordpress-access-post-meta-fields-through-wp-post/)

### Introduction

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.

To visualize the contents of these variables you can simply *[print_r()](https://www.php.net/manual/en/function.print-r.php)* or *[var_dump()](https://www.php.net/manual/en/function.var-dump.php)* them.

{% gist https://gist.github.com/vanaf1979/fa0c5d770b1e8398322a5865a89880e5 %}

And you would get an output similar to the output shown below.

{% gist https://gist.github.com/vanaf1979/f499c037871ab927a874111e0298eddf %}

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.

### use get_post_meta()

If you want to access a post meta value in your code you would normally use the *[get_post_meta()](https://developer.wordpress.org/reference/functions/add_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()](https://developer.wordpress.org/reference/functions/add_post_meta/)* function to add a *some_meta_field* meta field and give it a value of *some_value*.

{% gist https://gist.github.com/vanaf1979/a3330b5b7045c2ca2d1d808cec11833e %}

We can than use the *get_post_meta()* function to access the *some_meta_field* on the post.

{% gist https://gist.github.com/vanaf1979/0eff95f11e1b9a9d5596801232735f92 %}

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](https://developer.wordpress.org/reference/classes/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()*](https://www.php.net/manual/en/language.oop5.overloading.php#object.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*.

{% gist https://gist.github.com/vanaf1979/0382ec0b15cbb16d9f13d65905652795 %}

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.

{% gist https://gist.github.com/vanaf1979/6fa1c51da8c6290c3693213cfcb6053c %}

This will return *'some_value'* just like the *get_post_meta*() function did but as you can see this looks a lot cleaner.

### Other values

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.

{% gist https://gist.github.com/vanaf1979/d83ac62df26c2a06614404c3cc71d359 %}

### Conclusion

Accessing post meta fields through the *WP_Post* object could clean up your code quite a bit. But there may be cases where this is not the best approach so, as always, use it wisely.

**Follow me on twitter** [**@Vanaf1979**](https://twitter.com/Vanaf1979) **or on Dev.to** [**@Vanaf1979**](https://dev.to/vanaf1979) **to be notified about new articles, and other WordPress development related stuff.**

**Thanks for reading.**