Support

Account

Home Forums ACF PRO Expire Posts Reply To: Expire Posts

  • None of these worked sadly.. I don’t understand the issue with my code. The posts aren’t changing to Expired when they should do.

    // Setup and Run Cron Job
    if (!wp_next_scheduled( 'hrl_expire_listings' )) {
    	wp_schedule_event( time(), 'daily', 'hrl_expire_listings' );
    }
    add_action( 'hrl_expire_listings', 'listing_expiry_date' );
    
    // Setup Cron Job Function to expire posts and send out emails.
    function listing_expiry_date() {
    	
    	global $post;
    	
    	// Custom Post Type for listings, grab published posts
    	$args = array(
    		'post_type'      => array( 'post_type_listings' ),
    		'post_status'    => array( 'Publish' ),
    		'posts_per_page' => -1,
    	);
    	
    	// The Query
    	$query = new WP_Query( $args );
    	
    	// The Loop
    	if ( $query->have_posts() ) :
    		while ( $query->have_posts() ) :
    			$query->the_post();
    			
    			// Get listing author details
    			$author_display_name = get_the_author_meta( 'display_name' );	
    			$author_email        = get_the_author_meta( 'user_email' );
    
    			// Get admin/site details
    			$admin_email     = get_option('admin_email');
    			$admin_sitename  = get_option('blogname');	
    						
    			// Get the current date
    			date_default_timezone_set('Europe/London');
    			$today = date('Y-m-d');	
    				
    			// Get listing post published date (not pending date)
    			$published_date = get_the_date( 'Y-m-d' );	
    			
    			$expiry_date = date('Y-m-d', strtotime($published_date.' +5 days'));
    			
    			// If todays date is equal to or greater than the expiry date...
    			if ( $today >= $expiry_date ) :
    			
    				// change post status to 'expired'
    				$postdata = array(
    					'ID'          => $post->ID,
    					'post_status' => 'expired',
    				);			
    			
    			endif;								
    
    		endwhile; //endwhile The Loop
    		wp_reset_postdata();
    	endif; //endif The Loop		
    		
    } //end function listing_expiry_date()