Image fields not carried over when duplicating a post

Support General Image fields not carried over when duplicating a postResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #37013
    Sam UnderwoodSam Underwood
    Participant

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

    #37028
    Long NguyenLong Nguyen
    Moderator

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

    #37080
    Sam UnderwoodSam Underwood
    Participant

    Unsure 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

    #37084
    Long NguyenLong Nguyen
    Moderator

    Hi Sam,

    Thanks for more information.

    I think the helper function rwmb_set_meta() does not work in this case because the field image_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/#data

    So you can try to use the WordPress function add_post_meta() with the last parameter set to false 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/

    #37154
    Sam UnderwoodSam 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.

    #37176
    Long NguyenLong Nguyen
    Moderator

    Hi,

    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 function get_post_meta() to get raw value (image IDs),

    #37194
    Sam UnderwoodSam Underwood
    Participant

    It worked perfectly.
    I didn't need to use init for my case though.
    Thanks!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.