0
votes

I'm writing a very simple function in Wordpress that outputs the contents of a location field.

It works in the template file, but when I put it into functions.php and call it from the template, it stops working.

function check_location() { 
$location_field = get_post_meta($post->ID, 'location', true);   
return $location_field;
}

I did a var_dump and it returns the $location_field as a string(0)

I've also tried declaring global $post in the function, but it makes no difference.

Does anybody know what could be wrong?

2

2 Answers

0
votes

Your function missing actual post id as get_post_meta require post_id as the first input

check the following link to get current post id using get_the_ID() function https://developer.wordpress.org/reference/functions/get_the_id/

0
votes

Are you using default custom fields? or Advanced Custom fields. if you are using default meta fields then you can dump all fields using this

`<?php

$meta = get_post_meta(get_the_ID());
var_dump($meta);

?>`

And if you are using Advanced Custom Fields plugin, just using get_field('field_name', $post_id).