Skip Navigation

[Resolved] Start date and expiration date to the front end

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

Problem:
A Toolset Form is used to submit posts, and contains fields for start-date and end-date which should be used to automatically set the published status of the post.

Solution:
Toolset includes a feature to expire posts after a certain interval, but not to "enable" them.

You will first want to enable post expiration on Toolset Forms at Toolset > Settings > Forms.

(See https://toolset.com/documentation/user-guides/automatic-post-expiration/.)

Then on an individual form you can set a default interval such as "posts expire 60 days from now" then that will be applied to all posts submitted with the form.

Once such posts are submitted, if you edit the posts in the backend you will see when the post is due to expire, and you can update it from the post edit screen when you save the post.

The expiry date is stored as a UNIX timestamp in wp_postmeta for the post, with the key "_cred_post_expiration_time".

So you can customise this using the cred_save_data hook (https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data).

So you have your end-date field in the form. You can add a code snippet triggered by the cred_save_data hook which updates the _cred_post_expiration_time post meta according to what was chosen in the form.

An example of such code is given in the thread.

This support ticket is created 6 years, 5 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 23 replies, has 3 voices.

Last updated by pascalH 6 years, 5 months ago.

Assisted by: Nigel.

Author
Posts
#1077337

Hi Nigel

I posted a post yesterday at 15:30 (at front end):
Start date and time: same day time 16:00
End date and time: same day time 16:30

Could it be that it is not placed because the time is too short? Perhaps with regard to the cron job interval?

#1077345

Yesterday I saw the post not visible on the website, for example on the homepage. Also the test post toolset does not.
I will try again today and let you know as soon as possible.

Thanks in advance,
Greetings Pascal

#1077369

Hello Nigel

I just posted a post via the front end:
Titel: “Limonade Test offer Pascal”
Date and start time: Today 08/08 time 16:00
Date and end time: Today 08/08 time 17:00

I see on the backend the following:
Date and start time: Today 08/08 time 16:00 (same = ok)
Date and end time: Today 08/08 time 17:00 (same = ok)

Expiration of the post is showing: 08/08 19:00

The expectation:
1) The post will be displayed at various locations on the front end, for example the main page and page offers (aanbiedingen) and my account page (my offers - mijn aanbiedingen).
2) The post wil automatically be delete after 19:00.

Let's wait until 16:00 (Dutch time) and look wat will happen.

keep fingers crossed 🙂

#1077462

Hello Nigel

The post has not been published at 16:00
When I look in the back end, I see the post is still scheduled, nothing is shown on the front end of the website.

#1077497

Hello Nigel

I wonder if using WP Cron this is stable enough, I understand that when there are several CRON jobs at the same time that problems can arise (We do not want to miss any posts).

Let's review what we want exactly, to be sure, we may be doing too much work:

- A post (offer) may be placed directly (visible on the website must be the start date and end date) this was also the case at first.

- When creating the post is also chosen an end date and end time (this is the End date and time until when the offer is valid).

- Through the post changing form a user can change the offer, also the end date and time.

The post must be automatically deleted as soon as possible after the chosen End date and end time (for example Enddate + time + 2 hours or if not possible the day after the end date).

Is there maybe another way to do this? Or do you still think the best way is to get the Cron Job working?

Greetings Pascal

#1077546

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Cron jobs are the way to go, there isn't really an alternative, but this should be working, I have something similar (not exactly the same) on my test site and it is working okay.

I noticed that your settings are to run the cron job only once a day (screenshot). You probably want to change that to hourly, go to Toolset > Settings > Forms.

I'm not sure why the scheduled post isn't appearing as published once the publication date/time has passed.

There could be a discrepancy relating to timezones, I'll take a look in the morning.

#1077607

Hi Nigel

I start my computer in the evening and saw that the post that I posted earlier that afternoon was on the website.
I was surprised and at the same time enthusiastic. I have read your message and the CRON settings were indeed on 1x per day. When I changed this to 1 x per hour the postl disappeared immediately.

I have adjusted the CRON setting again to 5 minutes and create a new post.
Title: Test pascal Bread Date 08/08 time 20:45 End date 08/08 end time 21:00

But the post is not shown on the website yet, the time it is now 21:27

Just a question, not meant wrong but how stable is CRON?
I want to be sure that I will not have any problems in the future regarding posting and deleting posts.
The user of the website has paying customers.

If it is not stable and it is better to manually remove the posts then I will have to discus this with the user.
I did not expect it to be so complicated.

You do a really fantastic job, I'm waiting for your message

Greetings Pascal

#1078415

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi Pascal

I did some more testing and I found a couple of typos and also I confirmed that there was an issue relating to timezone settings.

I updated the code to the following:

/**
 * Set custom expiry date based on form field
 */
function tssupp_custom_expiry( $post_id, $form_data ){
  
    $begins_field = 'wpcf-startdatum-aanbieding';
    $expires_field = 'wpcf-einddatum-en-tijd-aanbieding';

    $offset_hours = get_option( 'gmt_offset' );
    $offset_secs = $offset_hours * 60 * 60;
  
    if ( in_array( $form_data['id'], array( 349, 885 ) ) ) { 
  
        if ( isset( $_POST[ $expires_field ] ) ) {

            $expires = $_POST[ $expires_field ]['timestamp'];
  
            $expires_gmt = $expires - $offset_secs;

            update_post_meta( $post_id, '_cred_post_expiration_time', $expires_gmt );
        }

        if ( isset( $_POST[ $begins_field ] ) ) {
 
            $begins_timestamp = $_POST[ $begins_field ][ 'timestamp' ];

            $begins = date('Y-m-d H:i:s', $begins_timestamp );

            $begins_gmt = date('Y-m-d H:i:s', ( $begins_timestamp + $offset_secs ) );
 
            wp_update_post( array
                (
                    'ID'            =>   $post_id,
                    'post_date'     =>   $begins,
                    'post_date_gmt' =>   $begins_gmt
                )
            );
        }
    }
}
add_action( 'cred_save_data', 'tssupp_custom_expiry', 10, 2 );

I tested this locally and it worked very much as expected, so hopefully if you make some new test posts you should see everything working fine too.

(I'm stopping now for a few days holiday, so if you have a new issue with this you may want to start a new thread so a colleague can help you.)

#1080604

Unfortunately it did not work out. When a post is placed it does not appear on the website at the set time.
I decided to leave it for what it is, the project must be completed as soon as possible. Perhaps we will be able to find out this later on a test domain.

Thank you in any case for the work and help you have done.
Have a nice holiday

Greetings Pascal