📖 Scenario: You are building a simple WordPress plugin to store and display extra information about blog posts. This extra information is called post meta. It helps you add custom details to posts, like a subtitle or a rating.
🎯 Goal: Learn how to add, update, and retrieve post meta data in WordPress using PHP functions. You will create a plugin that saves a custom subtitle for a post and then shows it on the post page.
📋 What You'll Learn
Create a post meta key and value for a post
Set a variable for the post ID
Use WordPress functions to add and update post meta
Retrieve and display the post meta value in the post content
💡 Why This Matters
🌍 Real World
Post meta is used to add extra information to WordPress posts, like custom fields, ratings, or subtitles, which helps customize content display.
💼 Career
Understanding post meta is essential for WordPress developers to extend site functionality and create custom content features.
Progress0 / 4 steps
1
Set the post ID variable
Create a variable called $post_id and set it to the number 42. This will represent the ID of the post you want to add meta data to.
Wordpress
Hint
Use $post_id = 42; to store the post ID.
2
Create the post meta key and value
Create two variables: $meta_key with the value 'subtitle' and $meta_value with the value 'A simple guide to WordPress'. These will hold the meta key and its value.
Wordpress
Hint
Use $meta_key = 'subtitle'; and $meta_value = 'A simple guide to WordPress';.
3
Add or update the post meta
Use the WordPress function update_post_meta with the variables $post_id, $meta_key, and $meta_value to add or update the post meta for the post.
Wordpress
Hint
Call update_post_meta($post_id, $meta_key, $meta_value); to save the meta.
4
Retrieve and display the post meta
Use the WordPress function get_post_meta with $post_id and $meta_key to get the meta value. Store it in a variable called $subtitle. Then echo the subtitle inside a paragraph tag: <p>Subtitle: <?php echo $subtitle; ?></p>.
Wordpress
Hint
Use $subtitle = get_post_meta($post_id, $meta_key, true); and then echo it inside a paragraph.
Practice
(1/5)
1. What is the main purpose of post meta in WordPress?
easy
A. To change the post title
B. To store extra information about posts
C. To delete posts permanently
D. To create new post categories
Solution
Step 1: Understand what post meta stores
Post meta is used to save additional data related to posts beyond the main content.
Step 2: Identify the correct purpose
Since post meta holds extra info, it does not change titles, delete posts, or create categories.
Final Answer:
To store extra information about posts -> Option B
Quick Check:
Post meta = extra post info [OK]
Hint: Post meta = extra data about posts [OK]
Common Mistakes:
Confusing post meta with post content
Thinking post meta changes post titles
Mixing post meta with categories
2. Which function is used to add a new meta value to a post in WordPress?
easy
A. get_post_meta()
B. update_post_meta()
C. delete_post_meta()
D. add_post_meta()
Solution
Step 1: Recall functions for post meta
Adding new meta uses add_post_meta(), getting uses get_post_meta().
Step 2: Match function to action
Since the question asks about adding, add_post_meta() is correct.
Final Answer:
add_post_meta() -> Option D
Quick Check:
Add meta = add_post_meta() [OK]
Hint: Add meta = add_post_meta() function [OK]
Common Mistakes:
Using get_post_meta() to add data
Confusing update_post_meta() with add_post_meta()
Trying to delete meta with add_post_meta()
3. What will the following code output?
echo get_post_meta(42, 'color', true);
Assuming post ID 42 has a meta key 'color' with value 'blue'.
medium
A. color
B. 42
C. blue
D. true
Solution
Step 1: Understand get_post_meta parameters
The function gets meta value for post ID 42, key 'color', with single value (true).
Step 2: Identify stored meta value
Since 'color' meta is 'blue', the function returns 'blue'.
Final Answer:
blue -> Option C
Quick Check:
get_post_meta returns stored meta value [OK]
Hint: get_post_meta returns meta value for key [OK]
Common Mistakes:
Confusing meta key with meta value
Expecting post ID as output
Misunderstanding the third parameter's effect
4. Identify the error in this code snippet:
update_post_meta( 'color', 42, 'red' );
medium
A. The post ID and meta key are swapped
B. The meta value is missing
C. Function name is incorrect
D. No error, code is correct
Solution
Step 1: Check parameter order for update_post_meta
The correct order is (post_id, meta_key, meta_value).
Step 2: Compare with given code
The code uses ('color', 42, 'red'), swapping post ID and meta key.
Final Answer:
The post ID and meta key are swapped -> Option A
Quick Check:
Parameter order matters in update_post_meta [OK]
Hint: Remember: post_id first, then meta_key, then meta_value [OK]
Common Mistakes:
Swapping post ID and meta key
Forgetting meta value parameter
Using wrong function name
5. You want to update a post meta key 'views' to increase its count by 1 for post ID 10. Which code snippet correctly does this?
hard
A. update_post_meta(10, 'views', get_post_meta(10, 'views', true) + 1);
B. add_post_meta(10, 'views', 1);
C. get_post_meta(10, 'views', true) + 1;
D. delete_post_meta(10, 'views'); update_post_meta(10, 'views', 1);
Solution
Step 1: Retrieve current 'views' meta value
Use get_post_meta(10, 'views', true) to get current count.
Step 2: Add 1 and update meta
Increase count by 1 and update with update_post_meta(10, 'views', new_value).
Final Answer:
update_post_meta(10, 'views', get_post_meta(10, 'views', true) + 1); -> Option A
Quick Check:
Increment meta by get + 1 then update [OK]
Hint: Get current value, add 1, then update meta [OK]
Common Mistakes:
Using add_post_meta instead of update_post_meta
Not retrieving current meta value before increment
Deleting meta unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.