Custom fields let you add extra information to your posts or pages. Displaying this data helps show more details to your visitors.
Displaying custom field data in Wordpress
<?php echo get_post_meta($post->ID, 'custom_field_name', true); ?>get_post_meta fetches the custom field value by its name.
The third parameter true returns a single value instead of an array.
<?php echo get_post_meta($post->ID, 'price', true); ?><?php $event_date = get_post_meta($post->ID, 'event_date', true); echo 'Event Date: ' . $event_date; ?>
<?php if ($note = get_post_meta($post->ID, 'note', true)) { echo '<p>Note: ' . esc_html($note) . '</p>'; } ?>
This code shows the post title, then checks if a 'subtitle' custom field exists. If yes, it displays the subtitle below the title, then shows the main content.
<?php /* Template part to display a custom field called 'subtitle' */ if (have_posts()) : while (have_posts()) : the_post(); the_title('<h1>', '</h1>'); $subtitle = get_post_meta(get_the_ID(), 'subtitle', true); if ($subtitle) { echo '<h2>' . esc_html($subtitle) . '</h2>'; } the_content(); endwhile; endif; ?>
Always use esc_html() or similar functions to safely output custom field data to avoid security issues.
Custom fields are stored per post, so use get_the_ID() or $post->ID to get the current post ID.
If a custom field is empty, check before displaying to avoid showing empty spaces.
Custom fields add extra info to posts or pages.
Use get_post_meta() to get and display this data.
Always check and sanitize the data before showing it.
Practice
Solution
Step 1: Identify the WordPress function for custom fields
The standard WordPress function to get custom field data isget_post_meta().Step 2: Confirm other options are not standard functions
Functions likeget_custom_field(),fetch_post_data(), andretrieve_meta()do not exist in WordPress core.Final Answer:
get_post_meta() -> Option AQuick Check:
Custom field retrieval = get_post_meta() [OK]
- Confusing function names with non-existent ones
- Using get_post_meta() without parameters
- Trying to access custom fields like normal variables
Solution
Step 1: Understand get_post_meta() parameters order
The function signature isget_post_meta($post_id, $key, $single). So the post ID comes first, then the key, then whether to return a single value.Step 2: Check which option matches this order
get_post_meta(42, 'color', true); usesget_post_meta(42, 'color', true);which is correct. Others swap parameters or miss the third argument.Final Answer:
get_post_meta(42, 'color', true); -> Option AQuick Check:
Correct parameter order = post ID, key, single [OK]
- Swapping post ID and key parameters
- Omitting the third parameter for single value
- Passing key as first argument
$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'?
Solution
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.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'.Final Answer:
25.99 -> Option DQuick Check:
Single meta value output = 25.99 [OK]
- Expecting an array instead of a string
- Not setting the third parameter to true
- Assuming NULL if value exists
echo get_post_meta('price', 15, true);Solution
Step 1: Check parameter order in get_post_meta()
The correct order isget_post_meta($post_id, $key, $single). Here, 'price' (key) is first and 15 (post ID) second, which is wrong.Step 2: Confirm other options are incorrect
The third parameter is present, the function exists, and post ID should be an integer, not string.Final Answer:
Parameters are in wrong order -> Option CQuick Check:
Post ID first, key second in get_post_meta() [OK]
- Swapping post ID and key parameters
- Assuming post ID can be string
- Omitting required parameters
Solution
Step 1: Retrieve 'rating' for current post with correct parameters
Useget_post_meta(get_the_ID(), 'rating', true)to get a single value for the current post inside the Loop.Step 2: Check if value exists and sanitize before output
Assign the value to a variable and check if it is truthy (not empty). Useesc_html()to safely display it.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.Final Answer:
if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); } -> Option BQuick Check:
Check existence + sanitize output = if ($rating = get_post_meta(get_the_ID(), 'rating', true)) { echo esc_html($rating); } [OK]
- Swapping post ID and key parameters
- Not checking if value exists before echo
- Not sanitizing output before display
Ready to prove it?
Pass the test and this pattern moves unknown
