Performance: Displaying custom field data
This affects page load speed and rendering performance by how custom field data is retrieved and displayed in WordPress templates.
<?php $custom_fields = get_post_custom(get_the_ID()); echo $custom_fields['custom_field'][0] ?? ''; ?>
<?php echo get_post_meta(get_the_ID(), 'custom_field', true); ?>| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple get_post_meta calls per post | Low | Low | Low | [X] Bad |
| Single get_post_custom call per post | Low | Low | Low | [OK] Good |
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