WordPress – Get Custom Field Inside and Outside The Loop

Getting custom fields in WordPress.

In the Loop: (Results for Posts within the Loop)

get_post_meta($post->ID, 'key name', true); //single result returned in a string

get_post_meta($post->ID, 'key name', flase); //all results returned in an array

Outside the Loop: (Result for Identified Post anywhere outside the loop ie. sidebar, header, footer)

global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'customField', true);

Leave a Reply

Your email address will not be published. Required fields are marked *