Home Forums ACF PRO Get values in a widget with get_post_meta()


Get values in a widget with get_post_meta()

  • Hi guys,

    I was looking for a way, but could not find a one yet. In widget, using get_field() we need to add the widget id as another parameter, but it won’t work in get_post_meta().

    Is it possible to get values in widgets with get_post_meta() instead of get_field()?

  • Hi @vasili

    This should be possible, but you will also need to get the page/post id and pass this as the first parameter. The code would look like so:

    global $wp_query;
    if(is_object($wp_query->queried_object) && $wp_query->queried_object->ID)
        echo get_post_meta($wp_query->queried_object->ID, 'field_name', true);
  • @acf-support

    Hi James

    Thank you for your reply.

    Unfortunately it did not work and returned nothing. I’m trying to pull the field value in a widget which is placed on the “Your latest posts” page as index.php.

    I’ve also tried using the get_option('page_for_posts'); and get_option('page_on_front'); to get the id of this page. If i just print the get_option() function it will return the id, but inside the get_post_meta() it always return an empty string.

  • @acf-support

    Hi James

    An Update!

    After further investigation i see that fields in widgets are stored as options in the wp_options table. I was able to pull the values using the following code:

    echo get_option('widget_' . $args['widget_id'] . '_field_name');

    The only thing is that field value is not changing live in Customizer, only after i hit the “Save & Publish” button and refreshing the page on the front end.

    More work needed with get_option().

  • Hi @vasili

    Thanks a lot for the follow up.

    I am glad you were able to load the value as expected.

    Will do some test on the same to verify the solution.

    Thanks for the feedback.

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

The topic ‘Get values in a widget with get_post_meta()’ is closed to new replies.