Reputation: 135
I’m starting to get into WP dev and I’m having issues with ACF Post Object returning null and I’m not sure why. I created a ACF with a location of Taxonomy is equal to Category Then the field is setup like…
Then in the post > category section I can edit the field and I select 3 posts. Finally in my category.php I try and dumb the values like so,
echo '<pre>';
var_dump(get_field('recommended_resources'));
echo '</pre>';
die();
and I get null Any help or point in the right direction is greatly appreciated.
Upvotes: 0
Views: 2475
Reputation: 1913
The problem is that you are not pointing the get_field
function to the correct post.
The field returns NULL because you want field recommended_resources
of post NULL.
If you look at the ACF get_field documentation, you'll see that it requires $id
of a post. https://www.advancedcustomfields.com/resources/get_field/
If on a post page or in a post loop:
echo '<pre>';
var_dump(get_field('recommended_resources', get_the_ID()));
echo '</pre>';
die();
just hardcoded:
$id = 216;
echo '<pre>';
var_dump(get_field('recommended_resources', $id));
echo '</pre>';
die();
How it was tested:
add_shortcode('test_test','test_test');
function test_test() {
$id = 216;
$post_obj = get_field('object', $id);
echo '<pre>';
print_r($post_obj);
echo '</pre>';
foreach($post_obj as $post) {
echo get_the_title( $post->ID ) . '<br>';
}
}
Upvotes: 1