Upload image through update_field in ACF

I am scraping an XML/RSS feed to create posts in WordPress using Advanced Custom Fields. Using the update_field is easy enough to update text and number fields but it looks like images are a different story. The image link we are pulling is a simple jpg but you can’t obviously load the url into the media library.

I have looked into saving the image into our database using a wordpress function then grabbing the attachment id and passing off to ACF. This function will take a little while to create so I am looking for potential otherways of handling this (if there is any).

Read More
// create new post
    $new_post = array(
        'post_title'    =>   $title,
        'post_status'   =>   'publish',
        'post_type'     =>   'media'
    );

    $post_id = wp_insert_post($new_post);

    wp_set_object_terms($post_id, $media_type, 'taxonomy_media');

    // use ACF API to update fields in post
    // update_field( $field key, $value, $post_id)

    update_field('field_xxxxxxxxxx', $title, $post_id);
    update_field('field_xxxxxxxxxx', $url, $post_id);
    update_field('field_xxxxxxxxxx', $image, $post_id); // wont work
    update_field('field_xxxxxxxxxx', $name, $post_id);

Anyone else ran into this problem and have a way of solving? Thanks

Related posts

Leave a Reply

2 comments

  1. You have to get the array of attachment IDs of images “already added to WP Media”.
    than add it to the acf gallery field like:

    $attachids = array("1", "2", "4", "5");
    
    update_field('field_xxxxxxxxxx', $attachids, $post_id);