Home Forums Add-ons Gallery Field Programmatically Inserting Photos Into ACF Gallery Field, But….


Programmatically Inserting Photos Into ACF Gallery Field, But….

  • …only the first photo shows up.

    I have a script that is supposed to upload photos from an external url, match them to the post it should belong to in WordPress, add them as a WP attachment, and then add the attachments to the gallery ACF field.

    And it works fine, except, when I go to the post in question in wp-admin, only the first photo in the gallery is actually in the gallery. If I check the database, I see no differences between the first photo and the rest of the photo, nor between manually uploaded gallery images and these. I have no idea what the disconnect is.

    Here is the relevant code:

    $uploads_dir = $_SERVER['DOCUMENT_ROOT'] . "" . "/" . $photo["photo"];
        $url = "{$photo["photo"]}";
        //$uploads_dir = $uploads_dir . "/" . $photo["photo"];
        file_put_contents($uploads_dir, file_get_contents($url));
        //attach the photos to the post
        // Check the type of file. We'll use this as the 'post_mime_type'.
        $filetype = wp_check_filetype( basename( $uploads_dir ), null );
        // Get the path to the upload directory.
        $wp_upload_dir = wp_upload_dir();
        // Prepare an array of post data for the attachment.
        $attachment = array(
            'guid'           => $wp_upload_dir['url'] . '/' . basename( $uploads_dir ), 
            'post_mime_type' => $filetype['type'],
            'post_title'     => preg_replace( '/\.[^.]+$/', '', basename( $uploads_dir ) ),
            'post_content'   => '',
            'post_status'    => 'inherit'
        $attach_id = wp_insert_attachment( $attachment, $uploads_dir, $post[0]->ID );
        require_once( ABSPATH . 'wp-admin/includes/image.php' );
        $attach_data = wp_generate_attachment_metadata( $attach_id, $uploads_dir );
        wp_update_attachment_metadata( $attach_id, $attach_data );
        //insert the ACF magic bits
        update_field("field_5693402ab8561", $attach_id, $post[0]->ID );

    At my wit’s end here. Any help would be greatly appreciated.

  • A gallery field in ACF stores an array of image IDs. I believe that the following line overwrites what’s stored with the new value each time.

    update_field("field_5693402ab8561", $attach_id, $post[0]->ID );

    You will need to get what’s already in the field, append the new image to the array and then update it. Something like this might work.

    $array = get_field('field_5693402ab8561', $post[0]->ID, false);
    if (!is_array($array)) {
      $array = array();
    $array[] = $attach_id;
    update_field('field_5693402ab8561', $array, $post[0]->ID );
  • I am in the process of needing to do something very similar to this. Did this end up working for you @jeffthefish?

    • tabboy

    • December 7, 2018 at 7:33 pm

    It almost works but because he is missing the “not” operator it fails to add the first entry. Here is the pseudo code.

    $gallery_array = get_field($key, $post_id, false);
    if (!is_array($gallery_array )) {
    	$gallery_array = array();
    $gallery_array [] = $img_attachment_id;
    update_field($key, $gallery_array , $post_id);
  • Sorry about the type in my code above, sometimes when I’m typing code into a comment here I can miss important things. A missing ! completely changes how it will work. I have edited my original comment.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.