Skip Navigation

[Résolu] Copy date from one date field to another when saving post

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

This topic contains 1 réponse, has 2 voix.

Last updated by Waqar Il y a 2 années et 2 mois.

Assigned support staff: Waqar.


I have two date fields set up- "from" and "to".

When creating a post with a CRED form, I want to copy the value entered in the "from" date field and save it in the "to" field.

I've tried the following (ignore form ID):

function cred_copy_date( $post_id, $form_data ) {       
	if ( $form_data["id"]==1 ) {
    	    $post_id = get_the_ID();
	    $date = $_POST['wpcf-from']['timestamp'];
	    update_post_meta( $post_id, 'wpcf-to', $date );
add_action( "cred_save_data", "cred_copy_date", 10, 2 );

I assume I'm not using the correct part of the date object array- what should I use? The timestamp? Just $_POST['wpcf-from'] ? Something else? I've tried several options and can't seem to find one that works.



Languages: Anglais (English )

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

Hi David,

Thank you for waiting.

During troubleshooting, I noticed that the function that you're using is correct, except for the following line:

$post_id = get_the_ID();

This line incorrectly replaces the newly entered post's ID, with the ID of the post/page, where the form is placed. As a result, the function "update_post_meta" updates the custom fields on the post/page with the form, instead of the newly added post.

Please remove that line and then check the form submission again.