Support

Account

Home Forums Add-ons Gallery Field Gallery field return images IDs

Solving

Gallery field return images IDs

  • Hello,
    I bought ACF PRO to use the gallery field but I have a problem with it.
    The field return an array of string (probably image’s ID) when i var_dump it.
    Obviously I would like to get at least url to display it, like in the documentation.

    <?php $images = get_field('img_event', $post->ID);
    var_dump($images);?>

    array(2) { [0]=> string(3) "360" [1]=> string(3) "292" }

    I have already installed my website on an other host but same thing.

    What is wrong ?

    Thank you

  • What do you have the field set to return, check your field settings.

  • I have no sensitive settings about the field (like the image field where you can chose how the field is displayed : array, url or id).

    I can just chose label, name, type (gallery), instructions, required, minimum and maximum selection, insert, library, restriction on size image, allowed file type, conditional logic and wrapper attribut.

    I don’t see what can change how the field is returned.

  • That’s strange, could have swore there was a setting on that field at one time. Been a while since I used an image field I guess. If the gallery field is only returning IDs this is usually caused by something else interfering. The question comes up here often, like this one https://support.advancedcustomfields.com/forums/topic/image-field-on-term-returning-id-instead-of-array/

    You’re going to need to narrow down where the interference might be coming from by disabling other plugins and possibly switching themes.

  • I have the same issue.
    I’ve tried on default ‘Twenty Seventeen’ and ‘Twenty Fifteen’ – the same.
    It seems that this settings just missing for gallery …

  • yes, this setting missing because in fact does not needs.
    Gallery returns array, if needs to get image IDs needs to use [‘ID’] as parameter, i.e. for example:

    <?php
       $images = get_field('gallery_field'); //gallery field
       foreach( $images as $img ):
       $img_srcset = wp_get_attachment_image_srcset($img['ID']); // return Image ID for each in gallery
    ?>
  • There is no setting for the gallery. It returns an array of image arrays. If you want to only return an array of IDs you can do

    
    $gallery = get_field('my_gallery_field', false, false);
    

    the 3rd parameter tells ACF not to format the value and this causes it to return just the list of IDs

    the 2nd parameter is the post ID, setting it to false just means the current post

    If you are trying to get the array of image arrays and acf is only returning the array of IDs anyway, this is almost always caused by a pre_get_posts filter that is incorrectly filtering the queries that ACF does to get the images. Usually because there is not sufficient logic to rule out these queries before making modifications.

  • sorry, yes, mistake.
    I meant Images IDs of gallery.

  • I had this problem too when registering the gallery field programatically.
    When I called :

    get_field('gallery_field_name',$post->ID)

    I got ID’s instead of image arrays.

    The Problem was, that I only registered the fields on the dashboard through checking for:

    is_admin()

    before calling:

    acf_add_local_field_group();

  • This reply has been marked as private.
  • i have the same issue. It returns the correct image array structur on my local dev server. when i try it on staging i only get an simple id array.

    i have allready uninstalled the acf pro plugin and deleted the field group i reinstalled acf and imported the fieldgroup from local via json file.

    still it behaves the same. all other plugins and settings are identical with local. no clue how to solve this 🙁

  • Ok i figured out something. Im adding the Gallery images (like all other data) through the API.
    When i store the post manually it works. When the data only comes from api it does not, even when the the data is stored in wp_postmeta EXACTLY the same way.
    So there has to be something that is stored with the editor, that the api does not store.
    But what could that be?

  • What API are you talking about?

  • I had a similar issue where ID’s were being returned even though the field was set to return image arrays.

    The problem was that I was importing content from another site and inserting an array of ID’s into the postmeta table with the key set to the field name as I had see it stored for other fields.
    (I’m guessing this is similar to the last post which mentions an API).

    Once imported the galleries showed normally in the admin area so I started working on the frontend and coded for what got returned, which was ID’s.
    I found that when I made a change to the post, the field then returned an array of images so my code stopped working.

    You need 2 meta fields for the gallery to return an array of images.
    The first one matching the field name which contains an array of image id’s and another with the same key starting with an underscore which references the ACF field ID.

    So in my case I have 1 field:
    sth_additional_images => {array of image ID’s}
    and a second:
    _sth_additional_images => field_5eb0a90d770ad

    So I adjusted the import to insert the second field at the same time, ran it again and then the gallery fields worked as expected.

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

The topic ‘Gallery field return images IDs’ is closed to new replies.