Home › Forums › General Issues › update image field with image from the wordpress media library
i have the following problem: i created an image-field with acf for my custom post type in which i want to store several hundred images.
1. i import these images with wordpress.
2. i run an import with the “really simple csv importer” in order to assign the images that i uploaded in the 1. step to the acf image field.
i tried a lot with the update_field() and the get_field_object() functions but nothing worked. the documentation for the update_field() function is unfortunately very incomplete, how do i have to use this function in order to update the image field?
More or less, you’re having a problem using update_field because the field does not exist yet. Get field object can’t get the field object because it does not exits, I think. When inserting new fields you need to use the field key. This is explained in the documentation for update_field().
According to the plugin it includes ACF support. Your best bet might be to ask in the support forum of that plugin.
are you sure, that it doesn’t exist? since it has a field-key i thought it would automatically exist, but without content. maybe that was wrong and you are totally right.
however, i also experimented with an already created post and tried to do the same thing: update the image-field with an image from the wordpress media library, without success since i don’t understand the update_field function parameter $value. i figured out that i probably have to pass some kind of array to that function (like the one you get with get_field_object). however my php-skills weren’t enough to make that working.
could you explain me how to do that?
i also asked at the support forum but didn’t get any response for days :/
$value depends on what type of field it is. If it’s an image field then you need to supply the image ID or the WP attachment ID value.
Are you using the field_key when calling update_field()?
update_field('key_1234567890123', $attachment_id, $post_id);
You said you have a lot of images, is the image field part of a repeater field? Is it some other kind of field like a gallery field?
thanks for the quick response. no, i’don’t use the repeater field. here is an example which might help to understand my approach:
– i build a custom post type for a special electronic device
– this post type consists of a lot of data: text-fields, dropdowns, checkboxes and one image field.
– my client has a csv table with all the different models every single model is one instance of this custom post type. that means, every post has it’s own image that i want to store in the image field.
if i need the image id this might further complicate things, since i don’t have the ids but the name or the url of the image files. but i will try to expreimentate with the ids to, that might be a good hint.
and yes, i use the field_key when calling update_field().
So, basically what you need to do is import the image into the media library https://codex.wordpress.org/Function_Reference/wp_insert_attachment
This will return the attachment ID and then you use the attachment ID when updating the image field.
As far as the first part goes, I don’t know how you’d accomplish that when importing from a csv.
If you have the name or url of the image, that is fine, use this function to reverse query the url to get the attachment id.
and here was my explanation and solution
I had the same issue on a past project where there were about 11,000 images, i knew the names of the images that came from the old system for a custom post, but i didnt know the new id when they were all in wordpress (i spent 2 hours manually uploading the images to know everything went through as i had issues with automated upload).
And this was a gallery of images for each post, so i did the following:
1. connected to old db and batch processed 200 posts from the old system at a time (with a start value and stop value for IDS of their old system posts), loading their custom image gallery
2. fed them into an relational array with the SKUID of their system as a key identifier – you end up with a massive relational array of SKUID and array of images in each array slot.
3. loaded the wordpress load.php
4. repeat through the array to get the custom SKUID of the post
5. load the wp post with corresponding skuid (ACF text field)
6. foreach loop on the images in the initial array with the correct SKUID
7. fed the image name with a prepended string for the wp-content folder
8. gave me back the correct id so i could then insert it in to another array
9. when ending the foreach loop i then assigned the array to the gallery field.
this script was run on the root of the wordpress installation and removed when finished.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
🚨 The 2023 ACF Annual Survey closes tomorrow! This is your last chance to complete the survey and help guide the evolution of ACF. https://t.co/0cgr9ZFOJ5— Advanced Custom Fields (@wp_acf) May 18, 2023
© 2023 Advanced Custom Fields.