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, 4 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
#956317

Q1 I am trying to create post expiry on post types after they are published. What is the best way to do it? Can I use the post expiration feature without CRED?

Q2 I am using the following code if I use the CRED but the problem is publication date is not always today (we want to change the publication date) How can I add publication date selection on CRED?

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);
#956367
Screen Shot 2018-07-26 at 3.22.07 PM.png
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']==35927)
    {
        $timestamp = get_post_meta($post_id, 'wpcf-user-note-publication-date', true);
        $date = date('Y-m-d H:i:s', $timestamp);
        if(isset($timestamp)){
            $args = array(
                'ID' => $post_id,
                'post_date' => $date,
                'post_status' => 'future'
            );
             wp_update_post( $args );
        }
    }
}

I used the code above but the status is still "published" even the date is in future.

Please see the attached image

#956372
Screen Shot 2018-07-26 at 3.51.52 PM.png

Additional details:

the information in database is correct but the post doesnt go to wordpress admin

#956388

Update:

Was able to add publication date in CRED with this code:

add_action('cred_save_data_35927', 'set_future_post_35927', 10, 2);
   
function set_future_post_35927($post_id, $form_data)
{
    $post_date1 = $_POST['wpcf-user-note-publication-date']['datepicker'];
    //$hours = $_POST['wpcf-user-note-publication-date']['hour'];
    // $hour = 6;
    //$mins = $_POST['wpcf-user-note-publication-date']['minute'];
    // $min = 0;
    $postfinaltime = $post_date1 + $hour + $min;
    $post_date = date("Y-m-d H:i:s", $postfinaltime);
    $gmt_date = gmdate("Y-m-d H:i:s", $postfinaltime);
  
  if ($post_date1){
    $my_post = array(
        'ID' => $post_id,
        'post_status' => 'future',
        'post_date' => $post_date, // Any future date having format 'Y-m-d H:i:s'
        'post_date_gmt' => $gmt_date,
         'edit_date' => true
    );

    // Update the post into the database
    wp_update_post( $my_post );
    }
  
  }

Q1: How can I set time to 6:00 AM?

The following code works but does not follow the publication date set on the above code, it follows the date today. For example, I set the post expiry to change fields on 5 minutes, even if the post is scheduled for another date it follows the date today and expires in 5 minutes after the post is added.

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

Q2: How can I set the default time of post expiry function to publication date not the date today?

#956603

Q1: How can I set time to 6:00 AM?
The basic datepicker creates timestamps at 12:00 AM GMT. If you want to add 6 hours to this timestamp, you must add the number of seconds in 6 hours: 6hrs * 60mins * 60secs = 21600.

$post_date1 = $_POST['wpcf-user-note-publication-date']['datepicker'];
$post_date_6am = $post_date1 + ( 6 * 60 * 60 );

Q2: How can I set the default time of post expiry function to publication date not the date today?
In your cred_save_data hook, you can set the expiration date manually like this:

    // modify the automated expiration date to use custom expiry date
    $expiry_date = $timestamp; // change this to be a timestamp for expiration, GMT
    update_post_meta($post_id, '_cred_post_expiration_time', $expiry_date );
  }
}
#957083

Hi Christian,

Thank you, but I think I am not clear with my 2nd question. I have no problem setting time of post expiry but with the publication date/date added of the post.

For example, I set the expiry to a (1) day. I added a post today but it is scheduled for tomorrow. The post will still expire tomorrow. It doesn't follow the publication date, it follows the date it was added.

#958284

There is currently no built-in way to modify the post publish date of a new post created with Forms. It must be modified using custom code. For example, you could add a generic date field to the Form, then use the cred_save_data hook to modify the post_date and expiration date based on that selected publish date.

add_action('cred_save_data', 'my_save_data_action',10,2);
function my_save_data_action($post_id, $form_data)
{
    // update post publish date based on generic date field
    $timestamp = $_POST['date-field-slug']['timestamp'];
    $date = date('Y-m-d H:i:s', $timestamp);
    if(isset($timestamp)){
        $args = array(
            'ID'           => $post_id,
            'post_date' => $date
        );
        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 );
    }
}
#1070699

Hi,

I was able to make your function and my previous 2 functions work together.

How can I set the expiration date automatic to 30 days after set (future) publication date?

Thank you!

#1071205

Hi again,

After testing for more example, I got an error. Please see my process below:

I am using:

add_action('cred_save_data_35927', 'set_future_post', 10, 2);
   
function set_future_post($post_id, $form_data)
{
    $post_date1 = $_POST['wpcf-user-note-publication-date']['datepicker'];
    $hours = $_POST['wpcf-user-note-publication-date']['hour'];
    $hour = $hours * 600;
    $mins = $_POST['wpcf-user-note-publication-date']['minute'];
    $min = $mins * 60;  
    $postfinaltime = $post_date1 + $hour + $min;
    $post_date = date("Y-m-d H:i:s", $postfinaltime);
    $gmt_date = gmdate("Y-m-d H:i:s", $postfinaltime);
  
  if ($post_date1){
    $my_post = array(
        'ID' => $post_id,
        'post_status' => 'future',
        'post_date' => $post_date, // Any future date having format 'Y-m-d H:i:s'
        'post_date_gmt' => $gmt_date,
         'edit_date' => true
    );

    // Update the post into the database
    wp_update_post( $my_post );
    }
  
  }



-----
add_action('cred_save_data_35927', 'set_expiration_date',10,2);
function set_expiration_date($post_id, $form_data)
{
	  // 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 );
    }
}

And deactivated the expiry settings on the CRED because it doesn't save the expiration date I set in the function.

The problem is how can I make the following code work without activating the post expiry settings on CRED?

function my_custom_expire_actions($custom_actions, $post_id, $form_data) {
        if ($form_data['id']== 35927){ 
	  	$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);

Coz right now it doesn't seem to work.

#1071496

The cred_post_expiration_custom_actions hook will not get triggered on future Form submissions if you turn off custom expiration on the Form, so you must leave it active. There's not a good way to programmatically set custom expiration actions outside of this hook, because the action information is serialized in the database. Are you saying that leaving the expiration action active on the Form is causing a problem with the custom expiration actions on the custom expiration date/time?

#1074438

Hi, I'm still testing. I'll update you back. Thank you.

#1075005

That sounds good, thanks. I'll stand by for your update.

#1076018
Screen Shot 2018-08-07 at 10.28.15 AM.png

Hi Christian,

Thanks for waiting.

If the "Set expiration date for post created or edited by this form" is checked this code doesn't work:

add_action('cred_save_data_35927', 'set_expiration_date',10,2);
function set_expiration_date($post_id, $form_data)
{
      // 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 );
    }
}

If the "Set expiration date for post created or edited by this form" is unchecked this code doesn't work:

function my_custom_expire_actions($custom_actions, $post_id, $form_data) {
        if ($form_data['id']== 35927){ 
        $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);

Also, the code above doesn't follow the publication date or the expiration date but the date post added, it is always the date today. Even if the post date is in future, it will still follow the date post is added and count from that instead of the date specified.

#1076419

If the "Set expiration date for post created or edited by this form" is checked this code doesn't work
Please remove the Form ID reference from the cred_save_data_35927 hook name. There is a quirk in the CRED API system that triggers specific Form hooks before some meta values are finalized, and it appears post expiration is one of those values. The cred_save_data hook is triggered a bit later in the request lifecycle, and will be able to override that value. Test the form ID in the callback instead, like this:

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

Also, the code above doesn't follow the publication date or the expiration date but the date post added, it is always the date today
If you're referring to the custom expiration time, I think changing the cred_save_data hook name will resolve this problem. The code I provided is based on the date selected in the user-note-expiration-date custom field, not based on the current date. If the custom expiration time is not calculated correctly, there must be a problem getting the value from the custom field. Please copy + paste the code you have used to insert this generic field into your Form.

#1077210
Screen Shot 2018-08-08 at 3.00.56 PM.png
Screen Shot 2018-08-08 at 3.00.50 PM.png

Hi again,

Still it is not working if the checkbox on post form expiration is <b>checked</b>

[credform class='cred-form cred-keep-original']

	[cred_field field='form_messages' value='' class='alert alert-warning']

Publication: [cred_field field='note-publication-date' value='' urlparam='' class='form-control' output='bootstrap']

Expiration: [cred_field field='note-expiration-date' value='' urlparam='' class='form-control' output='bootstrap']

	<div class="form-group">
		<label>Title</label>
		[cred_field field='post_title' value='' urlparam='' class='form-control' output='bootstrap']
	</div>

	<div class="form-group">
		<label>Content</label>
		[cred_field field='post_content' value='' urlparam='' output='bootstrap']
	</div>

	<div class="form-group" style="display:none;">
		<label>Note User</label>
		[cred_field field='note-user' value='[wpv-user field="user_login"]' urlparam='' class='form-control' output='bootstrap' type='hidden']
	</div>



	[cred_field field='form_submit' value='Submit' urlparam='' class='btn btn-primary btn-lg' output='bootstrap']

[/credform]