Skip Navigation

[Résolu] Set an expiration date based on form date in order to change pos status to trash

This support ticket is created Il y a 2 années et 1 mois. 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.

Aucun de nos assistants n'est disponible aujourd'hui sur le forum Jeu d'outils. Veuillez créer un ticket, et nous nous le traiterons dès notre prochaine connexion. Merci de votre compréhension.

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

Ce sujet contient 18 réponses, a 2 voix.

Dernière mise à jour par Waqar Il y a 2 années.

Assisté par: Waqar.

Auteur
Publications
#2467535
Capture d’écran 2022-09-30 102143.png

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"....

#2468923

Hello,
Were you able to make any progress on my request?

Jean

#2468943

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

#2471353

Hi and thanks for your response,

I'll try it and tell you if it works

Jean

#2471775

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);
                   }
        }

            
        }
    }
}




#2472045

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.

#2477565

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.

#2477637

Hi,
Yes you can do it.

#2478197

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.

#2480035

Just wanted to let you know that I'm still working on this and will share the findings, as soon as I can.

#2481085

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.

#2485335

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.

#2486155
erreur2.png
erreur1.png

So i'm back and the test did not work... the post is still published....

#2486595

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.

#2487487
vue2.png
vue1.png

Hi i did it so let's wait and see how it works....