Bird
Raised Fist0
Wordpressframework~20 mins

Displaying custom field data in Wordpress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Custom Field Data Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
1: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);
A25
BArray containing '25'
CThe string 'price'
DEmpty string
Attempts:
2 left
💡 Hint
Remember that the third parameter 'true' returns a single value, not an array.
📝 Syntax
intermediate
1: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.
Aecho get_post_meta(get_the_ID(), 'author_name');
Becho get_post_meta('author_name', get_the_ID(), true);
Cecho get_post_meta('author_name');
Decho get_post_meta(get_the_ID(), 'author_name', true);
Attempts:
2 left
💡 Hint
Check the order of parameters for get_post_meta function.
❓ state_output
advanced
2:00remaining
What will be the output of this code when the custom field 'rating' is not set?
Inside the Loop, consider this code:
$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';
ANo rating available
B0
CEmpty string
DPHP Notice error
Attempts:
2 left
💡 Hint
The ?: operator returns the right side if the left side is empty or false.
🔧 Debug
advanced
2:00remaining
Why does this code not display the custom field value?
This code is inside the Loop:
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);
AThe third parameter must be false to get a single value.
BThe parameters are in the wrong order; post ID must be first.
Cget_the_ID() returns null inside the Loop.
DThe meta key 'price' does not exist.
Attempts:
2 left
💡 Hint
Check the order of parameters in get_post_meta function.
🧠 Conceptual
expert
2: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.
Aget_post_meta always returns a string regardless of the third parameter.
Bget_post_meta requires the meta key to be the first parameter.
Cget_post_meta returns an array by default unless the third parameter is true.
Dget_post_meta cannot be used inside the Loop.
Attempts:
2 left
💡 Hint
Think about the return type of get_post_meta with and without the third parameter.

Practice

(1/5)
1. What is the main function used in WordPress to retrieve custom field data for a post?
easy
A. get_post_meta()
B. get_custom_field()
C. fetch_post_data()
D. retrieve_meta()

Solution

  1. Step 1: Identify the WordPress function for custom fields

    The standard WordPress function to get custom field data is get_post_meta().
  2. Step 2: Confirm other options are not standard functions

    Functions like get_custom_field(), fetch_post_data(), and retrieve_meta() do not exist in WordPress core.
  3. Final Answer:

    get_post_meta() -> Option A
  4. Quick Check:

    Custom field retrieval = get_post_meta() [OK]
Hint: Remember: WordPress uses get_post_meta() for custom fields [OK]
Common Mistakes:
  • Confusing function names with non-existent ones
  • Using get_post_meta() without parameters
  • Trying to access custom fields like normal variables
2. Which of the following is the correct syntax to get a single custom field value with key 'color' for post ID 42?
easy
A. get_post_meta(42, 'color', true);
B. get_post_meta('color', 42, true);
C. get_post_meta(42, 'color');
D. get_post_meta('color', 42);

Solution

  1. Step 1: Understand get_post_meta() parameters order

    The function signature is get_post_meta($post_id, $key, $single). So the post ID comes first, then the key, then whether to return a single value.
  2. Step 2: Check which option matches this order

    get_post_meta(42, 'color', true); uses get_post_meta(42, 'color', true); which is correct. Others swap parameters or miss the third argument.
  3. Final Answer:

    get_post_meta(42, 'color', true); -> Option A
  4. Quick Check:

    Correct parameter order = post ID, key, single [OK]
Hint: Post ID is first, key second, true for single value [OK]
Common Mistakes:
  • Swapping post ID and key parameters
  • Omitting the third parameter for single value
  • Passing key as first argument
3. Given the code snippet:
$value = get_post_meta(10, 'price', true);
echo $value;

What will be the output if the custom field 'price' for post ID 10 is set to '25.99'?
medium
A. Error: Undefined variable
B. Array
C. NULL
D. 25.99

Solution

  1. Step 1: Understand get_post_meta() with single=true

    When the third parameter is true, get_post_meta() returns a single value as a string, not an array.
  2. Step 2: Check the stored value for 'price'

    The custom field 'price' for post ID 10 is '25.99', so echoing $value outputs '25.99'.
  3. Final Answer:

    25.99 -> Option D
  4. Quick Check:

    Single meta value output = 25.99 [OK]
Hint: Use true to get single value, not array [OK]
Common Mistakes:
  • Expecting an array instead of a string
  • Not setting the third parameter to true
  • Assuming NULL if value exists
4. Identify the error in this code snippet:
echo get_post_meta('price', 15, true);
medium
A. Function get_post_meta() does not exist
B. Missing third parameter
C. Parameters are in wrong order
D. Post ID must be a string

Solution

  1. Step 1: Check parameter order in get_post_meta()

    The correct order is get_post_meta($post_id, $key, $single). Here, 'price' (key) is first and 15 (post ID) second, which is wrong.
  2. Step 2: Confirm other options are incorrect

    The third parameter is present, the function exists, and post ID should be an integer, not string.
  3. Final Answer:

    Parameters are in wrong order -> Option C
  4. Quick Check:

    Post ID first, key second in get_post_meta() [OK]
Hint: Post ID always comes before key in get_post_meta() [OK]
Common Mistakes:
  • Swapping post ID and key parameters
  • Assuming post ID can be string
  • Omitting required parameters
5. You want to display a custom field 'rating' for the current post inside the Loop, but only if it exists and is not empty. Which code snippet correctly does this?
hard
A. echo get_post_meta('rating', get_the_ID(), true);
B. if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); }
C. if (get_post_meta('rating')) { echo get_post_meta(get_the_ID(), 'rating'); }
D. echo get_post_meta(get_the_ID(), 'rating');

Solution

  1. Step 1: Retrieve 'rating' for current post with correct parameters

    Use get_post_meta(get_the_ID(), 'rating', true) to get a single value for the current post inside the Loop.
  2. Step 2: Check if value exists and sanitize before output

    Assign the value to a variable and check if it is truthy (not empty). Use esc_html() to safely display it.
  3. Step 3: Evaluate options

    if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); } correctly does all steps. echo get_post_meta('rating', get_the_ID(), true); swaps parameters. if (get_post_meta('rating')) { echo get_post_meta(get_the_ID(), 'rating'); } uses wrong parameter order and no sanitization. echo get_post_meta(get_the_ID(), 'rating'); echoes without checking if value exists.
  4. Final Answer:

    if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); } -> Option B
  5. Quick Check:

    Check existence + sanitize output = if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); } [OK]
Hint: Assign and check value before echoing, sanitize output [OK]
Common Mistakes:
  • Swapping post ID and key parameters
  • Not checking if value exists before echo
  • Not sanitizing output before display
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard