Expiration date in custom field

Community Forums Forums Design Tips and Tricks Expiration date in custom field

This topic is: not resolved

This topic contains 2 replies, has 2 voices, and was last updated by  handig 5 months, 2 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #70276

    handig
    Participant
    Post count: 7

    Hi,
    Can’t make this code snippet to work with Custom Code of Prose.

    I was looking into setting an expiration date to posts so that they disappear when the date set is expired.

    I was first looking into this plugin, but it seems to use cron jobs instead of custom fields.

    So then I found this code here.

    I’m using Prose with WP 3.7 so I thought I could add it to custom code which I did as follow:

    /*
    This function assumes a custom field named 'expiration' with a human friendly date/time.
    */
     
    function is_post_expired($post_ID = null){
        
        if(!$post_ID) global $post;
        
        $post_ID = $post_ID ? $post_ID : $post->ID;
        
        //Human Friendly Expiration Date
        $expiration = get_post_meta($post_ID, 'expiration', true);
        
        //Adjust server time for your timezone
        date_default_timezone_set('American/New_York');
        
        $expiration_timestamp = strtotime($expiration);
        $time_left = $expiration_timestamp - time();
     
        if($time_left < 0):
            if(expire_post($post_ID))
                return true;
        endif;
            
    }
     
    function expire_post($post_ID){
        
        $args = array(
            'ID' => $post_ID,
            'post_status' => 'draft'
        );
        if(wp_update_post($args))
            return true;
    }

    I then set the custom fields to a date in the past like 10/10/2013 and 10 September 2012 but the posts are still shown.

    What is wrong with the code?

    http://test2.zzpmarketing.com/
    #70361

    emasai
    Participant
    Post count: 482

    Can’t tell you what is wrong with it, but for php coding questions I usually go to Stackoverflow.com for answers.


    Need Website Customization or a Responsive CSS fix? Contact Me
    Lynne emasai.com

    #70462

    handig
    Participant
    Post count: 7

    I’m not so php apt. I saw the code snippet and was hoping I could add it like this to the custom code of Prose. So was wondering if somebody could see if that needs some small adaption or isn’t a way to implement this?

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.