Support » Developing with WordPress » Trying to set wp post to draft mode based on datepicker value

  • I have a custom post type called coupons. All coupons contain an ACF field called ‘expiration_date’ which is a datepicker field. I am trying to use this field to set the post status to ‘draft’ after the expiration date passes. I’m not sure if I’m just not using the correct wp hook or if my code is incorrect, but its not working. Could anyone offer a suggestion? Many thanks in advance.

    add_action( 'save_post', 'remove_expired_coupons', 10, 2 );
    
    function remove_expired_coupons( $post_ID, $post )
    {
    $expiry = new DateTime();
    $expiry->setTime(0,0,0);
    $expiresValue = $expiry->format('Y-m-d');
    
        $args = array(
            'post_type' => 'coupon',
            'posts_per_page' => -1,
            'post_status' => 'publish',
            'meta_query' => array(
                array(
                    'key' => 'expiration_date',
                    'value' => $expiresValue,
                    'compare' => '<=',
                    'type' => 'DATE'
                ),
            ),
        );
    
    $posts = get_posts($args);
    foreach( $posts as $post ) {
        if(get_field('expiration_date', $post->ID)) {
            $postdata = array(
                'ID' => $post->ID,
                'post_status' => 'draft'
            );
            wp_update_post($postdata);
        }
    }
    } 
    • This topic was modified 9 months, 3 weeks ago by plop_suey.
Viewing 1 replies (of 1 total)
  • Something like this may be better handled as a WordPress cronjob set to ‘daily’. Using the wp_schedule_event() function ( looking at the code examples in The Codex and tutorials online for help as needed ) you will be able to run a function once per day to set all coupons which has reached their expiration date to expired.

    I’m not entirely sure why you’re attempting to do so during a save_post action. You may run into an infinite loop if one of the coupons ends up in your loop for whatever reason which is never good. Finally, It loos like, according to the ACF documentation for the date field it saves the date field as ‘YYYYMMDD’ which we won’t be able to query against using compare DATE. The meta_query DATE expects the value to be YYYY-MM-DD so we would need to compare against numeric values.

    Here’s what the scheduled event would look like ( you may want to test this using Advanced Cron Manager ). Do note that the below code is completely untested, you should have a backup of your database before running this code.

    /**
     * Schedule Expiration Coupon Event
     *
     * @return void
     */
    function wpf11144133_expire_coupon_schedule() {
    
    	// If our event is not yet scheduled, schedule it!
    	if( ! wp_next_scheduled ( 'wpf11144133_expire_coupons_job' ) ) {
    		wp_schedule_event( time(), 'daily', 'wpf11144133_expire_coupons_job');
    	}
    
    }
    add_action( 'plugins_loaded', 'wpf11144133_expire_coupon_schedule' );
    
    /**
     * Tell the job what it needs to do
     *
     * @return void
     */
    function wpf11144133_expire_coupons_work() {
    
    	// Today to YYYYMMDD - https://www.advancedcustomfields.com/resources/date-picker/
    	$today = date( 'Ymd', strtotime( 'now' ) );
    
    	$posts = get_posts( array(
    		'post_type'			=> 'coupon',	// Coupon post type
    		'post_status' 		=> 'publish',	// Only grab published posts
    		'posts_per_page'	=> -1,			// Grab all posts
    		'no_found_rows'		=> true,		// Don't calculate pagination, we don't need it
    		'update_cache'		=> false,		// Don't need to cache results
    		'meta_query'		=> array(
    			'relation'	=> 'AND',
    			array(
    				'key'		=> 'expiration_date'	// Only worry about posts where the meta key exists AND...
    				'comapre'	=> 'EXISTS',
    			),
    			array(
    				'key'		=> 'expiration_date',
    				'value'		=> $today,
    				'compare'	=> '<=',
    				'type'		=> 'NUMERIC',			// DATE expects YYYY-MM-DD which is not what ACF saves, comapre aginst numeric values instead
    			)
    		),
    	) );
    
    	if( ! empty ( $posts ) ) {
    
    		foreach( $posts as $post ) {
    
    			wp_update_post( array(
    				'ID'			=> $post->ID,
    				'post_status' 	=> 'draft',
    			) );
    
    		}
    
    	}
    
    }
    add_action( 'wpf11144133_expire_coupons_job', 'wpf11144133_expire_coupons_work' );
Viewing 1 replies (of 1 total)
  • The topic ‘Trying to set wp post to draft mode based on datepicker value’ is closed to new replies.