Home » WordPress » WordPress get_post_meta Example

About Era Balliu

Era Balliu
Era is a Telecommunications Engineering student, with a great passion for new technologies. Up until now she has been coding with HTML/CSS, Bootstrap and other front-end coding languages and frameworks, and her recent love is Angular JS.

WordPress get_post_meta Example

Let’s take it as if you’re writing a post in your WordPress website. Being the nice guy he is, WordPress would actually facilitate this for you and why not add some little perks such as you including your favorite song for the moment, or even your mood into your post. This is done through metadata. How exactly? Look below!
 
 
 
 
 

get_post_meta()

Metadata is handled through the usage of key-value pairs. You can store the name of the metadata in the key part of this pair, and store the information which will appear in the metadata list of the post with which it’s related in value.

To get a post’s metadata we use the WordPress function get_post_meta. It’s syntax goes like below:

get_post_meta ( int $post_id, string $key = '', bool $single = false )

As you see, this function takes three arguments. The first one is $post_id, which is what you will identify the post whose metadata you are looking for with. This parameter is obligatory and must be an integer.

Next up is $key, which is an optional parameter of the format string that represents the meta key to be retrieved. If you don’t specify this, it will return data for all the keys by default. The last parameter is $single, a boolean value that shows whether a single value is to be returned or not. This parameter is also optional. By default it will take the value FALSE.

Current post’s meta

You can use this function to perform a number of actions on the metadata of the current post, such as, but not limited to, getting the meta for all the keys, for a single key, or even getting the first value of the a key. Take a look at the code below:

<?php $meta = get_post_meta( get_the_ID() ); ?>

<?php $key_1_value = get_post_meta( get_the_ID(), 'key_1' ); ?>

<?php $key_1_value = get_post_meta( get_the_ID(), 'key_1', true ); ?>

The first line of code is how we get the meta for all the keys, which is getting the id of a specific post, and not specifying either of the two optional parameters.

Not very differently we get the meta for a single key, only this time we specify the key and leave the rest as it is.

If we only want the first value for this key we specified, we set the last parameter to TRUE and all is well. Easy, right?

Custom Fields Meta

What is done differently if you have added custom fields to your post? Let’s take the example where we have to get a thumbnail’s URL, which is stored in the custom field named thumbnailurl. The code would go like below:

<?php if ( get_post_meta( get_the_ID(), 'thumbnailurl', true ) ) : ?>
      <a href="<?php the_permalink() ?>" rel="bookmark">
        <img class="thumbnailurl" 
        src="<?php echo esc_url( get_post_meta( get_the_ID(), 'thumbnailurl', true ) ); ?>" 
        alt=" <?php the_title_attribute(); ?>" />
      </a>
<?php endif; ?>

get_post_meta() hacks

No meta field is found

In case you are searching for a given $key in a specific post, and that is not found, then you can get a return value in two forms, depending on what the set value of the parameter $single is. If this parameter is set to FALSE then you will get an empty array, otherwise, you will be given an empty string.

While both may be equally disheartening, the way the return value is presented to you may change how you approach the rest of your code. However, you can add the two lines of code below and keep working the same regardless:

if( ! get_post_meta( '1', 'non-existing_meta', true ) ) {}
if( ! get_post_meta( '1', 'non-existing_meta', false ) ) {}

Both lines will be run in case we don’t find metadata for a given post.

Meta value is an empty string

If by chance you have stored an empty string into your meta value, then you wouldn’t be able to retrieve this by using get_post_meta. Instead you can get the correct answer by using get_post_custom_keys(). You can do this like below:

if( ! in_array( 'given_key', get_post_custom_keys( '1' ) ) ) {}

This function would correctly find out if the meta has a value that translates to false, such as empty strings or FALSE as a boolean value.

get_metadata()

If you want to get the metadata of a certain object this time, and not a post, then the function you must use is get_metadata(). It’s syntax goes like below:

get_metadata($meta_type, $object_id, $meta_key, $single)

Two of this functions’ parameters are obligatory, that being $meta_type and $object_id. The first one is the type of object the metadata is for, therefore it’s format is a string. The other one shows the ID of this object, making it to be an integer.

The other two optional parameters are $meta_key, which shows the $key of the required metadata and retrieves metadata for all keys if left unspecified, and $single which is a boolean value that when set to true return only the first value for the specified metadata.

Download the source code

This was an example of get_post_meta() in WordPress.

Download the source code for this tutorial:

Download
You can download the full source code of this example here : get_post_meta
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Web Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. Building web apps with Node.js
2. HTML5 Programming Cookbook
3. CSS Programming Cookbook
4. AngularJS Programming Cookbook
5. jQuery Programming Cookbook
6. Bootstrap Programming Cookbook
and many more ....
I agree to the Terms and Privacy Policy

Leave a Reply

avatar

This site uses Akismet to reduce spam. Learn how your comment data is processed.

  Subscribe  
Notify of