Support

Account

Home Forums General Issues get_field('field', $page_id) not returning a value.

Solved

get_field('field', $page_id) not returning a value.

  • Hi,

    I’m using the Avada theme, with a custom section in combination with ACF to display a custom shortcode.

    I’ve made a couple of custom fields linked to the page post type.
    For my issue I only need 1 of the fields, this field returns a ID that I need.
    If I call the field in the custom section I get a result (15), so I know the field has a value.
    I also have a custom shortcode on the same page, I need to use that ID in the shortcode.
    I can give the shortcode a parameter for ID, but in that custom section, I can’t get the actual ID to put it in the shortcode.

    I can use:
    global $post; $page_id = $post->ID
    To get the page id and use that with:
    $id_i_need = get_field('customfieldname',$page_id);

    But $id_i_need is empty when I echo it. echo on $page_id gives me the correct page ID.
    And as earlier established, the custom field has a value (15) but why is get_field() in combination with the page id not working?

  • Is the custom field name correct? Perhaps you have a typo there?

  • Yes, the name is correct – I checked. I always copy-paste values like this – exactly to prevent typos of this kind.

  • I’m not quite understanding the problem here. First you say:

    If I call the field in the custom section I get a result (15), so I know the field has a value.

    And then you say:

    I can give the shortcode a parameter for ID, but in that custom section, I can’t get the actual ID to put it in the shortcode.

    Also, what do you get if you do var_dump($id_i_need)?

  • I can display the custom field on the page with the ACF option for my text element.
    -> this will display 15, hence my conclusion that the field has the correct value.
    But I can’t use that ACF option of the text element to put that value as a parameter on the shortcode.=> As in I can’t print/echo the id into the actual shortcode, if this would be the shortcode [shortcode id=”?”], then I can’t get the ACF value on the ? as I can only get that with the text element (which is a shortcode itself).

    So I need to get the field via the page id within the shordcode code.

    var_dump gives me: NULL

  • Hi

    There are a few variables here that could change the possible solutions.
    But have you checked the Page ID ( Post ID ). I see you use the global $post variable. The ID can be changed by Plugins and Themes during the “PHP page compiling process”. Maybe you can “hardcode” to test if everything is working fine. The Page ID you need can be found when editing the page in the backoffice ( WP Admin ).

    E.g. When editing a page you get the URL https://www.mywebsite.com/wp-admin/post.php?post=71 => 71 is the Post ID you need to pass in get_field( ‘mycustomfield’, 71 );

    If this works, you need to rethink the global $post; $post->ID logic… Something is changing it before you can use it.

    Hope this helps.

    Kind regards
    Matt

  • … the Post ID seems to be the issue.
    I’ll have to figure that one out.
    But seems unrelated to the ACF plugin.

  • Since you are using global $post one thing that occurred to me is if there are any custom queries on the page make sure that wp_reset_postdata() is called. It could be that somewhere in your template the global $post variable is being reassigned without being reset.

    Not sure if this is relevant but hope this is helpful!

  • This was the issue in the end, it wasn’t the lack of a post ID, but had multiple. A reset fixed all issues.

  • Is the name of the custom field correct? Is it possible that you’ve made a mistake?

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

You must be logged in to reply to this topic.