Concept Flow - Displaying custom field data
Start: WordPress loads post
Check if custom field exists
Get custom field
Display custom field value
End
WordPress loads a post, checks for a custom field, retrieves it if present, and displays its value.
<?php $value = get_post_meta($post->ID, 'my_custom_field', true); if ($value) { echo $value; } ?>
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Call get_post_meta with post ID and 'my_custom_field' | Returns value or empty string | 'Special offer' |
| 2 | Check if $value is truthy | $value = 'Special offer' | True |
| 3 | Execute echo $value | Outputs 'Special offer' | Displays 'Special offer' on page |
| 4 | End script | No more code | Finished displaying custom field |
| Variable | Start | After Step 1 | After Step 2 | Final |
|---|---|---|---|---|
| $value | undefined | 'Special offer' | 'Special offer' | 'Special offer' |
Use get_post_meta(postID, 'field_name', true) to get a single custom field value. Check if the value exists before displaying. If true is false, you get an array instead. Echo the value to show it on the page. This is how WordPress displays custom field data safely.
get_post_meta().get_custom_field(), fetch_post_data(), and retrieve_meta() do not exist in WordPress core.get_post_meta($post_id, $key, $single). So the post ID comes first, then the key, then whether to return a single value.get_post_meta(42, 'color', true); which is correct. Others swap parameters or miss the third argument.$value = get_post_meta(10, 'price', true);
echo $value;
echo get_post_meta('price', 15, true);get_post_meta($post_id, $key, $single). Here, 'price' (key) is first and 15 (post ID) second, which is wrong.get_post_meta(get_the_ID(), 'rating', true) to get a single value for the current post inside the Loop.esc_html() to safely display it.Pass the test and this pattern moves unknown