Support

Account

Home Forums General Issues get_post_meta doesn't work with images

Solving

get_post_meta doesn't work with images

  • I’m using genesis and have tried both get_post_meta and genesis_get_custom_field which previously worked with ACF however now they don’t and i have to change the code to get_field();

    Am i missing something here?

  • It depends on what you’re trying to get. ACF stores the attachment ID for an image field. If you need to switch from get_field() to get_post_meta() then you’ll need to do any formatting that ACF was doing on your own using functions like https://developer.wordpress.org/reference/hooks/wp_get_attachment_image_src/

  • This reply has been marked as private.
  • ACF fields work using standard WP functions for get_post_meta() and get_option(). The functions in ACF are only wrappers for these functions.

    The problem is that if you use them on a field and you don’t use the ACF functions that the values will not be formatted for you.

    For example, when you create an image field and set it to return a URL. ACF does not (and never has) stored the URL of the image in the field. ACF stores the attachment ID of the image. get_post_meta() is not a theme specific function, it is a WP core function and is always available.

    In order to get the URL when using get_post_meta() you need to do the work of getting the URL field yourself.

    
    $image_id = intval(get_post_meta('image_field'));
    if ($image_id) {
      $image = wp_get_attachment_image_src($image_id, 'medium');
      if ($image) {
        ?><img src="<?php echo $image[0]; ?> /><?php 
      }
    }
    

    The same is true of any field that stores anything that is not a simple text value, like multi-select, page link, repeaters, relationship, post object, checkbox, taxonomy, flexible content, file, gallery, true/false, user, google map, date picker, time picker, and date/time picker fields.

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

The topic ‘get_post_meta doesn't work with images’ is closed to new replies.