Post meta lets you store extra information about a post in WordPress. It helps you add custom details beyond the main content.
Post meta basics in Wordpress
add_post_meta($post_id, $meta_key, $meta_value, $unique = false); get_post_meta($post_id, $meta_key, $single = false); update_post_meta($post_id, $meta_key, $meta_value, $prev_value = ''); delete_post_meta($post_id, $meta_key, $meta_value = '');
$post_id is the ID of the post you want to add or get meta for.
$meta_key is the name of the meta field, like 'price' or 'rating'.
add_post_meta(123, 'color', 'blue');
$color = get_post_meta(123, 'color', true);
update_post_meta(123, 'color', 'red');
delete_post_meta(123, 'color');
This example shows how to add, get, update, and optionally delete a post meta field called 'rating' for post ID 10. It prints the original, current, and updated rating values.
<?php // Add a custom meta field 'rating' with value 5 to post ID 10 add_post_meta(10, 'rating', 5, true); // Get the 'rating' meta value $rating = get_post_meta(10, 'rating', true); // Update the 'rating' to 8 update_post_meta(10, 'rating', 8); // Get the updated 'rating' $updated_rating = get_post_meta(10, 'rating', true); // Delete the 'rating' meta // delete_post_meta(10, 'rating'); // Output the values echo "Original rating: 5\n"; echo "Current rating: $rating\n"; echo "Updated rating: $updated_rating\n";
Use add_post_meta with $unique = true to avoid duplicate meta keys.
get_post_meta returns an array if $single is false, or a single value if true.
Always sanitize and validate meta values before saving for security.
Post meta stores extra info about posts in WordPress.
Use add_post_meta, get_post_meta, update_post_meta, and delete_post_meta to manage meta.
Meta keys are like labels, and meta values hold the data.
Practice
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 BQuick Check:
Post meta = extra post info [OK]
- Confusing post meta with post content
- Thinking post meta changes post titles
- Mixing post meta with categories
Solution
Step 1: Recall functions for post meta
Adding new meta usesadd_post_meta(), getting usesget_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 DQuick Check:
Add meta = add_post_meta() [OK]
- Using get_post_meta() to add data
- Confusing update_post_meta() with add_post_meta()
- Trying to delete meta with add_post_meta()
echo get_post_meta(42, 'color', true);
Assuming post ID 42 has a meta key 'color' with value 'blue'.
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 CQuick Check:
get_post_meta returns stored meta value [OK]
- Confusing meta key with meta value
- Expecting post ID as output
- Misunderstanding the third parameter's effect
update_post_meta( 'color', 42, 'red' );
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 AQuick Check:
Parameter order matters in update_post_meta [OK]
- Swapping post ID and meta key
- Forgetting meta value parameter
- Using wrong function name
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 AQuick Check:
Increment meta by get + 1 then update [OK]
- Using add_post_meta instead of update_post_meta
- Not retrieving current meta value before increment
- Deleting meta unnecessarily
Ready to prove it?
Pass the test and this pattern moves unknown
