Skip Navigation

[Resolved] Custom post expiration and future publish date in Forms

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

Problem: I have a Form that is used to 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. When the post expires, I would like to modify a custom field value.

Solution:
Add the following code to modify the publication date and post status:

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-user-note-expiration-date']['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 );
        }
    }
}

Add this code to modify the custom field value when the post expires:

function my_custom_expire_actions($custom_actions, $post_id, $form_data) {
    $custom_actions[] = array( 'meta_key' => 'wpcf-membership-type', 'meta_value' => 'Charter' );
    return $custom_actions;
}
  
add_filter('cred_post_expiration_custom_actions', 'my_custom_expire_actions', 10, 3);

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data
https://toolset.com/documentation/user-guides/automatic-post-expiration/

This support ticket is created 6 years, 3 months 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)

Tagged: 

This topic contains 21 replies, has 2 voices.

Last updated by randallH-3 6 years, 3 months ago.

Assisted by: Christian Cox.

Author
Posts
#1077480

Okay is it possible for me to log in and see how this is set up? If so, please provide login credentials here. Let me know which Form to check, and a URL where it can be seen on your site.

#1078569

1. This Form should probably be set to create posts in "Draft" status, since you will modify them in your cred_save_data hook to be published in the future. hidden link

2. The snippet that changes the custom expiration date isn't doing anything for two reasons. First, there is no Form with ID 35927. If you want to use this code with the Form on your test page, that ID should be 6484: hidden link
Second, the custom date field slug "wpcf-user-note-expiration-date" does not match any of the field slugs in the Form shown on the test page, so the timestamp is not based on any datepicker. You should be sure the field slug is identical to the field slug used in the Form.

#1080725

Hi,

I didn't notice that I used my other website's details.

Thank you. Still checking. Will let you know the results.

#1081375

Thanks, I'll stand by for your update.

#1084106
Screen Shot 2018-08-16 at 9.35.33 AM.png

Hi Christian,

Sometimes it doesn't work. Please check the membership type, it should be "Charter" and sometimes it misses the schedule. What could be the problem?

#1084650

If the post isn't published, then it can't expire, and therefore the CRED expiration actions won't fire. So the missing "Charter" role issue is probably just a symptom of the "Missed Schedule" error. The "Missed Schedule" error typically happens when the WordPress publishing "cron" does not run during the time period when the post should be published. This can happen for many reasons, but usually it's because you have your post scheduled for a specific time, like Monday at 1am, but no one visits the site from Sunday 10pm until Monday 10pm, skipping the scheduled publication time. I'm not exactly sure how long the interval must be, but after a certain amount of time has passed since the scheduled publication time, WordPress marks the post as "Missed schedule" so the site admin is aware the post was not published on schedule, and can then take action to publish it or reschedule it.

WordPress publishing crons can only be triggered by an HTTP request to your site. In other words, if no one interacts with the site, the publishing cron is never triggered. WordPress crons are not true crons, which run on timed intervals regardless of site traffic. My first suggestion is to set up a true cron job on your server that calls the WordPress cron (/wp-cron.php?doing_wp_cron) every few minutes. This will trigger the WordPress cron automatically. You may need assistance from your hosting company to set this up. Let's see if the true cron resolves the "missed schedule" problem. Then we can check to see if the Charter role is being applied correctly.

Once your true cron is started, remember that it may take some time before your cred expiration tasks are completed, depending on when you visit the site in relation to the true cron job. The amount of time could be as much as (true cron interval) + (toolset cred expiration cron interval).

#1084878

Thank you so much.