Setting custom fields on posts is one of the most important features of WordPress.
Developers use this functionality to save data on posts that can be retrieved easily for later usages.
This WordPress tutorial will try saving a custom field on a post and then retrieving it in the frontend.
How to set a custom field for a post in WordPress?
WordPress lets moderators set custom fields when they are editing a post in the admin panel.
There is also another way, Using functions to set the custom field for the post.
We’re going to check out both.
1- Using WordPress admin panel (in 3 Steps)
Setting a custom field in the admin UI is the preferred way for moderators.
When you are editing a post in the WordPress admin panel, look for the “Custom Fields” box under the editor:
Click “Enter new” in the custom fields box:
Then you can type a name and value and click “Add Custom Field”:
In case you did not find the Custom Fields box when editing the post in WP:
If you do not see the Custom Fields box, you must check the “Custom Fields” option in the Screen Options (old WP versions) or
Preferences -> Panels -> Additional (newer versions, using Gutenberg editor).
Enable Custom Fields in Screen Options (Classic WP Editor):
Look for the “Screen Option” button on the top right location of your WordPress post edit page. It is next to the “Help” button.
Enable Custom Fields in Post Preferences (Gutenberg Editor):
To enable custom fields in the Gutenberg editor, you must open the preferences settings, like bellow:
After opening the preferences popup, Select “Panel,” and then enable the “Custom Fields” option:
2- Using PHP codes
Functions are easier for me to explain! 🙂
If you know the post ID, you want to set the custom field for, use the
update_post_meta function to add your custom field.
update_post_meta function will check the custom field existence; if it’s existed, the function will update it; if not, the function will add it.
update_post_meta function syntax
I’m going to explain this function first, and then I’ll try it in an example.
update_post_meta($post_id, $meta_key, $meta_value, $prev_value = '')
$post_id Is obvious. It’s the ID of the post you want to set the custom field for.
$meta_key is the name of the custom field you want to set.
$meta_value Is the value of the custom field.
$prev_value is not a required parameter, but it’s a tricky one.
If you set the
$prev_value parameter, you actually are referring to a custom field with a specific value (to update it!).
$prev_value is useful when you want to manage multiple custom fields with the same name.
$prev_value, you are using the previous value as the reference to distinguish between the available custom fields with this name.
For a simple example, keep reading…
Set or update a custom field using the
When you know the syntax, it is easy to set a custom field for your posts.
For example, you can set a custom field with the name “my_rating” and value “4” in a post with an ID of 1:
$post_id = 1; $meta_key = "my_rating"; $meta_value = 4; update_post_meta($post_id, $meta_key, $meta_value);
How to get a custom field of a post in WordPress?
get_post_meta function syntax
get_post_meta function can retrieve the custom field value of a post for you.
get_post_meta($post_id, $key = '', $single = false)
$key is the name of the custom field you want to retrieve for the
$single is a boolean flag; If you set it as false (default), the result will be an array; If true, the result will be a single value.
get_post_meta usage example
In this example, we’re going to retrieve what we set in the previous example.
We’ll retrieve the my_rating custom field from a post with an ID 1:
$post_id = 1; $key = "my_rating"; $single = true; //it is a single custom field $my_rating = get_post_meta($post_id, $key, $single);