Sauter la navigation

[Résolu] Post expiration based on a custom datepicker field

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

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

Marqué : 

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

Dernière mise à jour par Lara Il y a 4 années et 7 mois.

Assisté par: Shane.

Auteur
Publications
#1654589

Tell us what you are trying to do?

I try to change the following code in a way, that the expiration date is not based on the future publication date like in the example - instead it should be based on the timestamp of a custom datepicker field.

What I found in the documentation:

I have a Form that create posts. I would like to schedule those posts for future publication, and I would like to set a custom post expiration date based on the future publication date.

add_action('cred_save_data', 'set_expiration_date',10,2);
function set_expiration_date($post_id, $form_data)
{
    $forms = array( 35927 );
    if (in_array($form_data['id'], $forms)) {
  
        // update post publish date based on generic date field
        $timestamp = $_POST['wpcf-ablauftermin']['datepicker'];
        $date = date('Y-m-d H:i:s', $timestamp);
        if(isset($timestamp)){
            $args = array(
                'ID' => $post_id,
            );
            wp_update_post( $args );
            // update post expiration date based on generic date field and time calculation
            $expiry_timestamp = $timestamp + ( 6 * 60 * 60 );
            update_post_meta($post_id, '_cred_post_expiration_time', $expiry_timestamp );
        }
    }
}

My changed code:

add_action('cred_save_data', 'set_expiration_date',10,2);
function set_expiration_date($post_id, $form_data)
{
    $forms = array( 35927 );
    if (in_array($form_data['id'], $forms)) {
  
        // try to load custom datepicker field 
        $lastday = $_POST['wpcf-ablauftermin']['datepicker'];

        if(isset($lastday)){
            $args = array(
                'ID' => $post_id,
            );


            // update post expiration date based on generic date field and time calculation
            $expiry_timestamp = $lastday + ( 6 * 60 * 60 );
            update_post_meta($post_id, '_cred_post_expiration_time', $expiry_timestamp );
        }
    }
}

Did I made any major mistakes in the code?

Is there any documentation that you are following?
https://toolset.com/forums/topic/post-expiry-for-post-types-not-using-cred-post-forms/

Is there a similar example that we can see?
In the documentation

What is the link to your site?
lien caché

#1654603

Shane
Supporter

Les langues: Anglais (English )

Fuseau horaire: America/Jamaica (GMT-05:00)

Hi Lara,

Thanks for getting in touch.

What I see here is that you didn't change the form ID in your code. You still have it as 35927 on line 4. You need to change this to the ID of your form itself.

Thanks,
Shane

#1656363

Ouch. You are right. I forgot to change the form ID. How embarrassing ...
Thanks Shane 🙂 Now it works fine.