0

I had the idea to make sticky posts expire after a certain period of time.

FYI, I am using a custom post type along with custom field suite and created sticky posts by creating a boolean custom field. premium is a true/false custom field which returns the value 1 if the checkbox is checked (which makes a post a featured post)

If I created another custom field to set the expiration date, how would I set the sticky-custom-field value to 0 when the expiration date is reached?

What I mean is (in written english):

  • check for a custom field with the key "premium"
  • check if custom field "premium" has value 1
  • check for a custom field with the key "expiration_date"
  • check if "expiration_date" value is in the past, but not today
  • if "expiration_date" is in the past, set "premium" value to 0

Can someone assist making this a function?

I'd begin with the following (please excuse the style, I am just trying to provide ideas)

global $post;
global $cfs;
if ( '1' = $cfs->get('premium') && ' ' != $cfs->get('expiration_date') ) :
$current_date = date(Y-m-d);
$expiration_date = $cfs->get('expiration_date');
if( // I'm stuck here... ) ....
date_post_meta( $post->ID, 'premium', '1', '0' ); // set premium field from 1 to 0 for every post that matches
endif;
  • It depends on the format of your expiration_date but basically you need to compare dates. Like e.g. here stackoverflow.com/questions/2113940/php-compare-date – kraftner Jul 29 '14 at 12:15
  • if both the $current_date and $expiration_date are formatted YYYY-mm-dd... could I do this:$current_date = date(Y-m-d); $expires = $cfs->get('expiration_date'); $today = strtotime($current_date); $expiration_date = strtotime($expires); and then do if ($expiration_date < $today) ) : update_post_meta($post->ID, 'premium', '1', '0'); endif; ? Please have a look at my code, I feel there could be mistakes. – okiedokey Jul 29 '14 at 13:51
  • Not sure why you would need to have a 1 or 0 returned? Couldn't you just say $today = date("ymd"); and then if($expiry > $today) (with expiry also in ("ymd") format) then show post? – Bysander Jul 29 '14 at 16:38
  • Sorry just re-read - I gather that premium is something like sponsored posts? – Bysander Jul 29 '14 at 16:40
  • @Bysander premium is a true/false checkbox custom field created by Custom Field Suite. I check it when I want a post to be a featured post and it then returns the value 1. sorry. I edited that in my above post. – okiedokey Jul 30 '14 at 15:02
0

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;
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.