Set custom field value when reaching expiration date

Got it. Plugin developer helped me looking at my suggested code.

First, make sure you declare global $cfs (necessary when using Custom Field Suite Plugin).

The following code checks for custom field named premium and if its value is set to 1 (which means it’s supposed to be a premium post) and also for a custom field named expiration_date and if a value is set.

The code then formats today’s date properly to match the date format of the custom field, which is Y-m-d in this case.

After we defined and formatted the dates, the code checks if the expiration date is in the past ( $expiration_date < $today ) and updates the value of the premium field from 1 to 0, which means it unsets the posts premium status.

The code:

global $cfs; //necessary for custom field suite plugin
if ( '1' == $cfs->get('premium') && '' != $cfs->get('expiration_date') ) : // if post has premium status and an expiration date set
    $current_date = date('Y-m-d'); // format today's date
    $expiry = $cfs->get('expiration_date');
    $today = strtotime($current_date);
    $expiration_date = strtotime($expiry);

    if ( $expiration_date < $today ) { // if expiration date is in the past
        $field_data = array('premium' => '0'); // unset premium status
        $post_data = array('ID' => $post->ID);
        $cfs->save($field_data, $post_data);
    }
endif;