Tell us what you are trying to do?
I want to Set an expiration date based on form date in order to automatiquely change post status to trash
Is there any documentation that you are following?
i use this code :
//Set the post expiry date to the "réfugiés" field
add_action('cred_save_data', 'calculate_post_expiration_func',9999,2);
function calculate_post_expiration_func($post_id, $form_data) {
// if a specific form
$forms = array(29497,30773);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dateevenement']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
}
}
It works because an expiration date is set but the option set status is "keep original status" and not "trash"....
Hello,
Were you able to make any progress on my request?
Jean
Hi,
Thank you for contacting us and I'd be happy to assist.
The value for the "Set status" field on post-expiration is saved in the custom field with the key '_cred_post_expiration_action'.
To change that value to be set to 'trash' the post on expiration, you can extend your custom function to update that custom field value as well.
For example:
//Set the post expiry date to the "réfugiés" field
add_action('cred_save_data', 'calculate_post_expiration_func',9999,2);
function calculate_post_expiration_func($post_id, $form_data) {
// if a specific form
$forms = array(29497,30773);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dateevenement']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
// get current value for the post expiration action field value
$post_status_action = get_post_meta( $post_id, '_cred_post_expiration_action', true );
// if not empty and if set to 'original', set to change to 'trash' status
if(!empty($post_status_action)) {
if($post_status_action['post_status'] == 'original') {
$post_status_action['post_status'] = 'trash';
update_post_meta($post_id, '_cred_post_expiration_action', $post_status_action);
}
}
}
}
I hope this helps and please let me know if you need any further assistance around this.
regards,
Waqar
Hi and thanks for your response,
I'll try it and tell you if it works
Jean
Hi Waqar,
I tried it out with no success.... i made changes but the status doesn't change, it stay at original....
here is my code :
//Set the post expiry date to the "réfugiés" field
add_action('cred_save_data', 'calculate_post_expiration_func',9999,2);
function calculate_post_expiration_func($post_id, $form_data) {
// if a specific form
$forms = array(29497,30773);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dateevenement']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
// get current value for the post expiration action field value
$post_status_action = get_post_meta( $post_id, '_cred_post_expiration_action', true );
// if not empty and if set to 'original', set to change to 'trash' status
if(!empty($post_status_action)) {
if($post_status_action['post_status'] != 'trash') {
$post_status_action['post_status'] = 'trash';
update_post_meta($post_id, '_cred_post_expiration_action', $post_status_action);
}
}
}
}
// if a specific form
$forms = array(30522,30776);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dates-actu-refugi']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
// get current value for the post expiration action field value
$post_status_action = get_post_meta( $post_id, '_cred_post_expiration_action', true );
// if not empty and if set to 'original', set to change to 'trash' status
if(!empty($post_status_action)) {
if($post_status_action['post_status'] != 'trash') {
$post_status_action['post_status'] = 'trash';
update_post_meta($post_id, '_cred_post_expiration_action', $post_status_action);
}
}
}
}
}
Thanks for the update. it is strange that the code is not working.
Can you please share temporary admin login details, along with the link to the page where this form can be seen?
Note: Your next reply will be private and making a complete backup copy is recommended before sharing the access details.
Thank you for sharing the access details.
I've performed a number of tests on my website, with a similar setup as your website, and the exact same code is working as expected. This suggests that something specific to your website is involved.
Do I have your permission to download the clone/snapshot of your website? This will help in investigating this on a different server.
Thank you for the permission. I've downloaded the website's duplicator package.
I'll be running some tests on this and will share the findings as soon as this testing completes.
Thank you for your patience.
Just wanted to let you know that I'm still working on this and will share the findings, as soon as I can.
Thank you for waiting.
During troubleshooting, I noticed that the code to change the post-expiration status to 'trash' was working for the 'edit post' forms, but not for the 'create post' forms.
The reason was that the code would check for the existing post-expiration status value, but for the newly created posts, it was empty.
I updated the code to not check for the existing post-expiration status value and it started working for both types of forms:
//Set the post expiry date to the "réfugiés" field
add_action('cred_save_data', 'calculate_post_expiration_func',9999,2);
function calculate_post_expiration_func($post_id, $form_data) {
// if a specific form
$forms = array(29497,30773);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dateevenement']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
// set expiration action to 'trash' status
$new_status_arr = array( 'post_status' => 'trash', 'custom_actions' => array() );
update_post_meta($post_id, '_cred_post_expiration_action', $new_status_arr);
}
}
// if a specific form
$forms = array(30522,30776);
if ( in_array($form_data['id'], $forms) )
{
// event end date timestamp
$end_date = $_POST['wpcf-dates-actu-refugi']['datepicker'];
// if end date is set
if(isset($end_date) && !empty($end_date)){
// add 1 day to the end date
$exp_date = strtotime('+1 day', $end_date);
// set new date as post expiration date
update_post_meta($post_id, '_cred_post_expiration_time', $exp_date);
// set expiration action to 'trash' status
$new_status_arr = array( 'post_status' => 'trash', 'custom_actions' => array() );
update_post_meta($post_id, '_cred_post_expiration_action', $new_status_arr);
}
}
}
You can make similar changes in the code on your website too and it will work.
I have integrated the new code and the expiration dates are well taken into account and with put to trash. But the post that was supposed to be deleted was not and was still displayed, maybe because it had been published before the modification. I just put a test post and it too is scheduled to be trashed this Sunday at 2:00. I'll see if it works and come back to you.
So i'm back and the test did not work... the post is still published....
Thanks for the update that is strange.
Can you please do another quick test, but this time enabling the post-expiration option in the form's setting?
( you can set any fixed expiration time in the form's settings and our custom code will overwrite that time )
Please let me know how it goes.
Hi i did it so let's wait and see how it works....