Skip Navigation

[Resolved] Duplicate CPT in frontend – atachment repeating custom fields

This support ticket is created 2 years, 10 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 2 replies, has 2 voices.

Last updated by felixM-3 2 years, 10 months ago.

Assisted by: Waqar.

Author
Posts
#2050213

Hello, i try to duplicate ca CPT in frontend that heave several field ant a repeatable attachment custom field. I use this function to achieve that:

//* Duplicate CPT
add_action('cred_save_data_455', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
// get data of original post
$post = get_post( $form_data['container_id'], ARRAY_A );
// update the new post with this data
$post['ID'] = $post_id;
$post['post_title'] = 'CopyOf ' . $post['post_title'];
wp_update_post( $post );
// get fields of original post
$fields = get_post_custom( $form_data['container_id'] );
// update the new post with these fields
foreach ($fields as $key => $values) {
foreach ($values as $value) {
$value = maybe_unserialize( $value );
add_post_meta($post_id, $key, $value, true);
}
}
}

Everything is working fine but the repeatable attachment type custom field is not duplicated entirely when i have attached several files - it is duplicating only the first attached file.
Please help my to achieve that all the attached file to be duplicated.

Thank you

#2051343

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi,

Thank you for contacting us and I'd be happy to assist.

The 4th parameter "$unique" in the 'add_post_meta' function, decides whether the custom field with the same key should be added or not:
https://developer.wordpress.org/reference/functions/add_post_meta/

Since the repeating fields use the custom fields with the same key, you should replace 'true' with 'false', in your custom function:


add_post_meta($post_id, $key, $value, false);

I hope this helps and let me know how it goes.

regards,
Waqar

#2051419

My issue is resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.