dustin
dustin

Reputation: 135

ACF Post Object returning null

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.

Screenshot of local DB Screenshot of local DB

Upvotes: 0

Views: 2475

Answers (1)

Fresz
Fresz

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

Related Questions