Support

Account

Home Forums Backend Issues (wp-admin) The_field() not working with dynamic value on plugin integration

Solved

The_field() not working with dynamic value on plugin integration

  • Hi,

    I am trying to integrate custom field to an existing plugin.
    The plugin uses setup_postdata( $post );

    we can then use get_permalink() or what ever..

    I am trying to use advanced custom field on that loop.
    the_field('my_field', 123); works fine.

    but when I try this it dosen’t work
    the_field('my_field', $post->ID);
    or

    $current_ID = $post->ID;
    the_field('my_field',  $current_ID );

    I also tried that:

    $get_the_field = get_field('my_field', $post->ID) ; 
    echo $get_the_field; 

    why would it work when i input the number manualy and not when it’s a variable?

    i believe it’s because ACF is not properly initialized, how could i work around that, initialize it inside the plugin?

    EDIT:
    to complete the case when the field name is set by a variable and the post id is a number it works

    $field_name = 'my_field';
    echo get_field($field_name, 123);

    but the part i want to be dynamic is obviously the post id
    i tried to get the post id variable in various ways like (int)$post->ID;
    $post->ID;
    with no luck so far
    Thanks in advance for your help.

  • Hi @l.pirondini

    Your codes look good, but have you debugged the $post_id parameter? Perhaps $post->ID is not actually returning the correct ID?

    I would debug this to make sure that value you pass to get_field is correct.

    Thanks
    E

  • Hi Elliot, Thanks for your answer
    I did a var_dump() on the $post->ID and it outputs int(479) (or whatever the post number is) if i have a fix variable like $postID= int(479); it works.

  • Hi @l.pirondini

    Interesting. Your results above prove that the $post_id parameter should be correct.

    If your variable contains the correct ID, then this would be the same as manually entering the number. I can’t explain why it wouldn’t be working.

    perhaps the issue is something else? but I don’t understand what else could be the issue.

    Maybe you could post your full code?

    Thanks
    E

  • Hi Elliot, I will come back to you with the full code explained a little bit better.

  • Hi, I finally managed to get it working.
    the plugin I was modifying was trying to access the field data before it was saved. that’s why it worked with an already published post entering it’s post id number but not with a new one.

    the second issue i had have been solved to, i was developing a solution to publish a post to facebook using the advanced custom fields as description value or to set the image of the facebook post, i had a scope problem with that one.

    I will try to make that last one into a plugin, i diden’t found any existing plugin that let you use ACF values to publish a post to a social network

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘The_field() not working with dynamic value on plugin integration’ is closed to new replies.