WordPress provides various powerful functions that are really helpful in creating a user-friendly and optimized site. One such function is get_post_meta.
If you understand it properly, it can be a valuable way to enhance the functionality and customization of your site. WordPress uses it to store custom information about the posts. We can, at any time, retrieve this information with the help of the get_post_meta function and use it as we want.
This get_post_metafunction, allows you to fetch custom field data for posts and pages as per your requirement. It gives you the ability to add additional functionality to the post or page while developing your theme or plugin. While the functionality it brings is great, working with it can be a bit challenging at first but is not difficult once you understand it.
In this article, we will explain each essential concept of the get_post_meta function. Additionally, we will learn about its parameters and how to use them in different situations.
1. What Is ‘get_post_meta’?
Before jumping into the function itself, let’s understand its definition, and when to use it with a simple example.
It’s a built-in function used to retrieve data from custom fields associated with posts, pages, and other custom post types. Moreover, it allows you to add additional metadata to each post, enhancing flexibility and customizability.
At the core, any information that is attached to a post is known as post meta. For example, product description, product price, etc.
If you have ever used the Advanced Custom Fields plugin in WordPress. You may already have an idea about custom fields. The fields it provides is also post metadata.
With get_post_meta, you can pull this data dynamically into your WordPress template whenever and however you need. For instance, there can be situations where you are not satisfied with the default options and want to extend them.
In such cases, you can create custom fields and use them with this function. Although WordPress provides some post meta features already that are attached to a post or page etc.
2. Using ‘get_post_meta’ to Retrieve and Display Custom Fields
Let us break down how get_post_meta works, including its parameters and syntax, and provide a sample code for retrieving custom field values. The get_post_meta function uses three parameters:
1 2 3 |
get_post_meta($post_id, $meta_key, $single); |
As we can see in the above snippet, the get_post_meta function accepts three parameters. All three parameters are crucial to understanding how to use the function properly. So let us understand each of them with examples so that you can understand them properly.
1. $post_id
Definition: $post_id is the unique identifier for the post or page from which you want to retrieve metadata.
Purpose: This tells get_post_meta which specific post’s metadata you want to fetch. We will learn more about it in detail later. Every post, page, or custom post type in WordPress has a unique numeric ID, and this parameter targets a specific post. You can check this ID from the dashboard by hovering over it.
Inside the Loop: If you’re calling get_post_meta within The Loop, you can get the current post’s ID by using get_the_ID() . This will get the ID of the related post inside the loop.
Outside the Loop: When working outside The Loop, you may need to specify a post ID manually, especially if you want to fetch data for a specific post. For instance, we want to retrieve metadata of a page titled My Test Page, we can do it using the code below –
1 2 3 4 5 6 |
$page = get_page_by_title('My Test Page'); $meta_info = get_post_meta($page->id); // $layout_info will contain the metadata for the page titled 'My Test Page' |
2. $meta_key
Definition: $meta_key is the name of the custom field or metadata key you want to retrieve.
Purpose: This parameter specifies the exact custom field you want to access. When you add custom fields to posts, each field has a name (unique key) and a value. The $meta_key parameter tells get_post_meta which field’s value you want to retrieve for the specified post.
Important Note: The $meta_keyvalue must be the same as the key name you used when creating the custom field. For example, if you have created the custom field with the name author_name then you need to use the same as the $meta_key in the get_post_meta . Otherwise, the function won’t find the field and will return null or an empty value. For instance, let’s take the previous example. If we need the meta value of the key ‘layout’ in the page titled ‘My Test Page’, we can do this using the following code –
1 2 3 4 5 6 |
$page = get_page_by_title('My Test Page'); $layout_info = get_post_meta($page->id, 'layout'); // $layout_info will contain the metadata for the page titled 'My Test Page' |
3. $single
Definition: $single is a Boolean parameter that determines how the retrieved metadata should be returned.
Purpose: This parameter specifies whether you want to retrieve a single value or multiple values for the specified $meta_key.
If set to true, get_post_meta will return a single value as a string. This is ideal if you know there’s only one value associated with the $meta_key.
If set to false, the function will return an array of values. This is useful if there are various values for the same custom field key (for example, if you’ve added several values under the same custom field name). Expanding on the above example, let’s add this argument –
1 2 3 4 5 6 7 8 9 |
$page = get_page_by_title('My Test Page'); $layout_info = get_post_meta($page->id, 'layout', true); // $layout_info will contain the value of 'layout' meta field in string format $layout_info = get_post_meta($page->id, 'layout', false); // $layout_info will contain the value of 'layout meta field as an array, this the default behaviour |
3. Examples of ‘get_post_meta’ with simple real-world examples.
Example 1: Displaying an Author Name
if ($author_name = get_post_meta(get_the_ID(), 'author_name', true)) { echo 'Written by ' . esc_html($author_name); }
Example 2: Showing Product Specifications
$product_weight = get_post_meta(get_the_ID(), 'weight', true); echo 'Weight: '. esc_html($product_weight) . ' kg';
4. Final Thoughts
This was all about using the get_post_meta function in WordPress. Understanding it will surely help you work efficiently in the WordPress environment. This function can be used anywhere in the theme depending the functionality you require. In most cases, you will be using it in The Loop, so directly adding it in single.php, page.php or other files of the template hierarchy should help you get metadata information of the particular post in The Loop.
If you need to use it outside The Loop, you can use it in functions.php or in any PHP file in your theme. In case of using this function outside the loop, first parameter is mandatory. You need to provide the ID of a specific post whose metadata is to be retrieved. In the loop, WordPress assumes it’s the value of the current post.
You can use plugins that allow you to add the code as a snippet which is the easier and safer option. We recommend using the plugin WP Code as it is simple to work and gets the work done.