How to Put get_post_meta into a Variable from Outside The Loop

Today I needed to put a custom field value into a variable that was being defined outside The Loop in functions.php. I had originally had the following code:

// This code DOES NOT work
$my_variable = get_post_meta($post->ID, 'my_custom_field_value', true);

The solution was the following:

// This code DOES work
$my_variable = get_post_meta(get_the_id(), 'my_custom_field_value', true);


(Thanks Lasson!)

Posted in ,

Toby Cryns

Toby Cryns is a freelance CTO, expert WordPress consultant, and teacher.

He offers free advice to improve your freelance biz.

He also publishes small droppings every now and then to and

Follow Toby's contributions on Github and