Skip Navigation

[Resolved] Inserting images using add_post_meta

This support ticket is created 6 years, 2 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Our next available supporter will start replying to tickets in about 1.34 hours from now. Thank you for your understanding.

This topic contains 1 reply, has 1 voice.

Last updated by mayurJ 6 years, 2 months ago.

Author
Posts
#1111249

Tell us what you are trying to do?
===========================
Trying to add some images into a repeating image field using add_post_meta

Is there any documentation that you are following?
=========================================
Existing tickets

Is there a similar example that we can see?
Seems like this syntax isn't working properly. Can you confirm? Maybe it's how I'm passing on the array?

$image_urls = '<em><u>hidden link</u></em>';
// Turning comma separated string into an array 
$image_array = explode(",",$image_urls);
// Delete all values
delete_post_meta($new_post_id, 'wpcf-photo-gallery');
// Add new values
foreach($image_urls as $single_image_url) {
	add_post_meta($new_post_id, 'wpcf-photo-gallery', $single_image_url, false);
}
#1111283

I found the solution myself. Much simpler, and works perfectly for Image Repeating fields

// declare your array contents
$image_array[] = "your absolute URL here #1";
$image_array[] = "your absolute URL here #2";
//use as many as you want above.. place an absolute URL between the quotes. P.S. You don't need to index the array above with 0,1, etc.. PHP does that automagically for you

if (is_array($image_array)) {  //sanity test to make sure you didn't blow up the array above
	foreach($image_array as $single_url) {  // break the array down into single entries
		add_post_meta($YOUR_POST_ID, 'wpcf-YOUR-FIELD-SLUG', $single_url, false);  //add the contents to your custom post type's image field. 
	}
}
// replace YOUR-FIELD-SLUG, and YOUR_POST_ID above with whatever is appropriate.