Support

Account

Forum Replies Created

  • Well, still not sure why any of the code examples in the ACF forums like this post Expire post to draft or this post Expire Posts on Datepicker field
    aren’t working.

    Here’s what finally worked for me:
    Publish based on datepicker field “publish_coupon_date”

    add_action( 'wp', 'publish_coupons_daily' );
    function publish_coupons_daily() {
        if ( ! wp_next_scheduled( 'publish_coupons' ) ) {
            wp_schedule_event( strtotime('07:02:00'), 'daily', 'publish_coupons');//runs at or after 12:02am local time, server time is in UTC
        }
    }
    add_action( 'publish_coupons', 'publish_coupons_function' );
    
    function publish_coupons_function() {
        
        $date_default_timezone_set('America/Los_Angeles'); // ensures the comparison happens based on local timezone, not on server UTC time, so they don't get published too soon
        $today = date('Ymd');
        $args = array(
            'post_type' => array('coupon'), // post types you want to check
            'post_status' => 'draft', 
            'posts_per_page' => -1 
        );
        $posts = get_posts($args);
        
        foreach($posts as $p){
                
            $publishdate = get_field('publish_coupon_date', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field
    
                if (($publishdate > $today) || ($publishdate = $today)) { 
                    //if date is today OR prior to today
                    $postdata = array(
                        'ID' => $p->ID,
                        'post_status' => 'publish'
                    );
                    wp_update_post($postdata);
                 }      
        }
    }

    and expiring posts from datepicker field “expire_coupon_date”

    add_action( 'wp', 'expire_events_daily' );
    function expire_events_daily() {
        if ( ! wp_next_scheduled( 'delete_expired_events' ) ) {
            wp_schedule_event( strtotime('07:01:00'), 'daily', 'delete_expired_events'); //runs at or after 12:01am local time, server time is in UTC
        }
    }
    add_action( 'delete_expired_events', 'expire_coupon_function' );
    
    function expire_coupon_function() {
        date_default_timezone_set('America/Los_Angeles'); // see other note, we set this to ensure it expires on local time, not server time
        $today = date('Ymd');
        $args = array(
            'post_type' => array('coupon'), 
            'post_status' => 'publish',
            'posts_per_page' => -1 
        );
        $posts = get_posts($args);
        
        foreach($posts as $p){
                
            $expiredate = get_field('expire_coupon_date', $p->ID, false, false); // get the raw date from the db. false, false will convert to Ymd while allowing you to use any date format output choice on the field itself
    
                if (($expiredate < $today) && ($expiredate != "")) { // if date is less than today, but also not empty to accomodate the coupons that don't expire
                    $postdata = array(
                        'ID' => $p->ID,
                        'post_status' => 'draft'
                    );
                    wp_update_post($postdata);
                 }      
        }
    }

    have fun kiddies.

Viewing 1 post (of 1 total)