Skip Navigation

[Resolved] Post Modification Date from form field

This thread is resolved. Here is a description of the problem and solution.

Problem: I have a Form that contains a date field. I would like to use the date from the field as the post date. The posts are published in "Pending Review" status.

Solution: You can't set the publish date of a Pending Review post, but you can set the last modification date using this code:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
$forms = array( 123, 456, 789 );
if ( in_array( $form_data['id'], $forms) )
{
 
// nothing below here was changed for the comma-separated list of IDs
 
$timestamp = get_post_meta($post_id, 'wpcf-datum-des-bildes', true);
$date = date('Y-m-d H:i:s', $timestamp);
if(isset($timestamp)){
$args = array(
'ID' => $post_id,
'post_modified' => $date
);
wp_update_post( $args );
}
}

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

This support ticket is created 6 years 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
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 9 replies, has 2 voices.

Last updated by michaelM-30 6 years ago.

Assisted by: Christian Cox.

Author
Posts
#1149283

Tell us what you are trying to do?
When our CRED form for a new post is published, the actual date is used.
We need a different date - from one of the entries inside the form iteself.

Is there any documentation that you are following?
I was using this code:
https://toolset.com/forums/topic/publication-date-field-in-a-cred-form/

In the functions.php:
add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
if ($form_data['id']==1754)
{
$timestamp = get_post_meta($post_id, 'wpcf-datum-des-bildes', true);
$date = date('Y-m-d H:i:s', $timestamp);
if(isset($timestamp)){
$args = array(
'ID' => $post_id,
'post_date' => $date
);
wp_update_post( $args );
}
}

The CRED Field I would like to use as a base for the post date is this :

<div class="form-group">
<label>Datum der Aufnahme</label>
[cred_field field="datum-des-bildes" force_type="field" class="form-control" output="bootstrap"]
</div>

It is not working like this, is it because auf the wpcf?

Many thanks
Michael

#1149427

The "wpcf-" prefix is required in the code, so I don't think that's the problem. It looks like you have not updated the Form ID. Check here:

if ($form_data['id']==1754)

You should replace 1754 with the numeric ID of your Form. You can find the Form ID in Toolset > Post Forms. Then test again and let me know if the problem is not resolved.

#1149755
download.jpg

Thanks,
I changed the ID to the corresponding ID from the form page.

But it didn't change the post date accordingly.

I assume, if I have more form pages later on, I can separate the id's with ","?

thanks

#1149939

What is the status of posts created by this Form?

#1149946

The status was "for review" - without being saved again.

But I tested it with "direct post" - and it works.

The reason I was trying "for review" is , that users might make mistakes with my form.
Is it possible to get it working with "for review" also?

#1150132

Is it possible to get it working with "for review" also?
The code to update the post_date will not affect a pending post. The post_date sets the publish date, and a pending post is not published. Instead, you could update the post_modified date or the post_modified_gmt date:

$args = array(
'ID' => $post_id,
'post_modified' => $date
);
wp_update_post( $args );

https://stackoverflow.com/questions/17412202/how-to-change-the-modified-time-of-a-wordpress-post

#1153323

So is it possible to use more than one ID?
if ($form_data['id']==1754)
My try with multiple id's seperated with "," did not work.

thanks
Michael

#1153335

Not quite, but you can modify the conditions to test for a value in an array of multiple Form IDs:

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
// if a specific form
$forms = array( 123, 456, 789 );
if ( in_array( $form_data['id'], $forms) )
{

// nothing below here was changed for the comma-separated list of IDs

$timestamp = get_post_meta($post_id, 'wpcf-datum-des-bildes', true);
$date = date('Y-m-d H:i:s', $timestamp);
if(isset($timestamp)){
$args = array(
'ID' => $post_id,
'post_modified' => $date
);
wp_update_post( $args );
}
}

Now you can use a comma-separated list of IDs.

#1156124

Thanks for your help - it works.

But I do have another post id "wpcf-datum-des-dokuments" that I need the script to work with
(and another one)

Do I need another action for that?

#1157191

My issue is resolved now. Thank you!