Skip Navigation

[Resuelto] Duplicate post with todays date and +365 finish day

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
Duplicate post with todays date and +365 finish day using Toolset form

Solution:
You can use WordPress standard function update_post_meta() in order to update the custom field value using the Toolset form hook.

You can find proposed solution, in this case, with the following reply
https://toolset.com/forums/topic/duplicate-post-with-todays-date-and-365-finish-day/#post-919819

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created hace 6 años, 4 meses. 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

Este tema contiene 5 respuestas, tiene 2 mensajes.

Última actualización por rafaelE-3 hace 6 años, 4 meses.

Asistido por: Minesh.

Autor
Mensajes
#919273

I have a button to duplicate my posts from the frontend using this hook:

//Duplicate posts
add_action('cred_save_data_376', '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'] = 'Copia de ' . $post['post_title'];
$post['status'] = '_draft';

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) {
add_post_meta($post_id, $key, $value, false);
}
}
}

And this other Hooks to get todays date and +365 days:

//Todays date
add_shortcode('wpv-post-today', 'today_shortcode');
function today_shortcode() {
return time();
}

//+365 date
add_shortcode('wpv-post-masdias', 'masdias_shortcode');
function masdias_shortcode() {
return strtotime('+365 days', time());}

What I need is to duplicate the post and use the todays date for [wpcf-fecha-de-inicio'] (post start date) and +365 days date to [wpcf-fecha-de-fin] (Post end date).
How can I add this to the hook?

Thanks in advance!

#919819

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - what if you try to add following two lines of code within your duplicate_post function.

// added by toolset
update_post_meta($post_id,'wpcf-fecha-de-inicio',time());
update_post_meta($post_id,'wpcf-fecha-de-fin',strtotime('+365 days', time()));

Do you see the correct time is saved?

#919829

Thanks Minesh! It works like a charm! 😀

One thing more:
The post status of the copied post appears always as published.
Can you check my code and tell if I've made any mistake?

Thanks again for your help!

#919833

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Could you please try to use following line of code and try to resolve your issue:

$post['post_status'] = 'draft';
#919850

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

could you please confirm - the solution I shared works for you or not?

#919932

It works!

Great work, Minesh!

Thanks for your help!