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 3 replies, has 3 voices, and was last updated by  jamesparkin 1 year, 2 months ago.

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

    handig
    Participant

    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

    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

    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?

    #105627

    jamesparkin
    Participant

    Handig

    Have you been able to list yours posts in ascending order according to expiration date? I’ve been trying to get this code to work. Using Lifestyle Pro.

    $args = array( ‘post_type’ => ‘event’, ‘posts_per_page’ => 15, ‘order’ => ‘ASC’, ‘meta_key’ => ‘_expiration-date’, ‘orderby’ => ‘meta_value’ );
    $eventloop = new WP_Query( $args );
    if ( $eventloop->have_posts() ) : while ( $eventloop->have_posts() ) : $eventloop->the_post();

    //All the Loop Content Goes Here

    endwhile;
    endif;

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

You must be logged in to reply to this topic.