Challenge - 5 Problems
Custom Field Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate1:30remaining
What is the output of this WordPress code snippet?
Given a post with a custom field 'price' set to '25', what will this code output inside the Loop?
echo get_post_meta(get_the_ID(), 'price', true);
Wordpress
echo get_post_meta(get_the_ID(), 'price', true);Attempts:
2 left
💡 Hint
Remember that the third parameter 'true' returns a single value, not an array.
✗ Incorrect
get_post_meta with third parameter true returns the single value of the custom field, which is '25'.
📝 Syntax
intermediate1:30remaining
Which option correctly retrieves and displays a custom field 'author_name' in WordPress?
Select the code snippet that correctly fetches and echoes the 'author_name' custom field for the current post inside the Loop.
Attempts:
2 left
💡 Hint
Check the order of parameters for get_post_meta function.
✗ Incorrect
The correct order is post ID, meta key, and whether to return single value. Option D follows this order.
❓ state_output
advanced2:00remaining
What will be the output of this code when the custom field 'rating' is not set?
Inside the Loop, consider this code:
What will it output if 'rating' is missing?
$rating = get_post_meta(get_the_ID(), 'rating', true); echo $rating ?: 'No rating available';
What will it output if 'rating' is missing?
Wordpress
$rating = get_post_meta(get_the_ID(), 'rating', true); echo $rating ?: 'No rating available';
Attempts:
2 left
💡 Hint
The ?: operator returns the right side if the left side is empty or false.
✗ Incorrect
If the meta field is missing, get_post_meta returns an empty string, so the expression outputs 'No rating available'.
🔧 Debug
advanced2:00remaining
Why does this code not display the custom field value?
This code is inside the Loop:
Why does it not show the expected price?
echo get_post_meta('price', get_the_ID(), true);Why does it not show the expected price?
Wordpress
echo get_post_meta('price', get_the_ID(), true);Attempts:
2 left
💡 Hint
Check the order of parameters in get_post_meta function.
✗ Incorrect
get_post_meta expects post ID first, then meta key. The code has them reversed, so it fails to retrieve the value.
🧠 Conceptual
expert2:30remaining
Which statement about displaying custom field data in WordPress is TRUE?
Choose the correct statement about retrieving and displaying custom field data using get_post_meta.
Attempts:
2 left
💡 Hint
Think about the return type of get_post_meta with and without the third parameter.
✗ Incorrect
By default, get_post_meta returns an array of values for the meta key. Passing true returns a single value.