Image fields not carried over when duplicating a post
- This topic has 6 replies, 2 voices, and was last updated 2 years, 8 months ago by
Sam Underwood.
-
AuthorPosts
-
July 14, 2022 at 10:34 PM #37013
Sam Underwood
ParticipantThese lines duplicate ALL Metabox fields. But, for some reason, the images are missing:
// duplicate MetaBox fields $all_fields = rwmb_get_object_fields( $post_id ); foreach ($all_fields as $field_id => $field_value) { $group_value = rwmb_meta( $field_id, $args = '', $post_id); rwmb_set_meta($new_post_id, $field_id, $group_value); }
The field type that is not duplicating is - https://docs.metabox.io/fields/image-upload/
July 15, 2022 at 7:45 PM #37028Long Nguyen
ModeratorHi Sam,
Can you please share some screenshots of the issue? And the code that creates the custom fields on your site. I will check it on my end.
July 20, 2022 at 6:03 PM #37080Sam Underwood
ParticipantUnsure what you'd like screenshots of, I can share with you a video of the issue from the frontend if that would help?
The custom field code related to the images field:
[ 'id' => 'card_images', 'name' => 'Enter the Images', 'type' => 'image_upload', 'force_delete' => false, 'max_file_uploads' => 50, 'max_status' => false, 'image_size' => 'medium', 'hidden' => [ 'card_type', '!=', 'images' ] ],
The full code - https://pastebin.com/JPd3m7V9
July 20, 2022 at 9:58 PM #37084Long Nguyen
ModeratorHi Sam,
Thanks for more information.
I think the helper function
rwmb_set_meta()
does not work in this case because the fieldimage_upload
save meta values (field value) to the database in multiple rows with the same meta key (field ID). Read more here https://docs.metabox.io/fields/image-upload/#dataSo you can try to use the WordPress function
add_post_meta()
with the last parameter set tofalse
to update this field value for a post. I will inform the development team to support this case in the next update.
https://developer.wordpress.org/reference/functions/add_post_meta/July 24, 2022 at 12:04 AM #37154Sam Underwood
Participant// duplicate MetaBox images $images_value = rwmb_meta( 'card_images', $args = '', $post_id); foreach ( $images_value as $single_image ) { add_post_meta($new_post_id, 'card_images', $single_image, false); //rwmb_set_meta($new_post_id, 'card_images', $single_image); }
add_post_meta
doesn't seem to work. Maybe I'm doing something wrong?
rwmb_set_meta
kinda works, but it ends up only saving the last image.July 25, 2022 at 12:33 PM #37176Long Nguyen
ModeratorHi,
Here is the code that works on my local site
add_action( 'init', function() { ... }, 99 );
$images_upload = get_post_meta( $post_id, 'card_images' ); foreach ( $images_upload as $single_image ) { add_post_meta( $new_post_id, 'card_images', $single_image, false); }
Use the action
init
with priority 99 (later than 20) to make sure the helper functions are loaded before. Or use the WP functionget_post_meta()
to get raw value (image IDs),July 26, 2022 at 6:13 AM #37194Sam Underwood
ParticipantIt worked perfectly.
I didn't need to useinit
for my case though.
Thanks! -
AuthorPosts
- You must be logged in to reply to this topic.