Search code examples
phpwordpressadvanced-custom-fields

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).

// 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


Solution

  • Instead of uploading through the Wordpress gallery. I just ended up saving the url for the image and using that in my templates.