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
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.
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
What is the status of posts created by this Form?
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?
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
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
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.
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?
My issue is resolved now. Thank you!