Support

Account

Home Forums ACF PRO Expire Posts

Solving

Expire Posts

  • I am having issues with the functionality of my code. The custom post should expire after a certain number of days. At the moment it’s set to 3 days just so I can test it.
    They are expiring even though before the expiry date and I can’t figure out why?!

    // Setup Cron Job Function to expire posts and send out emails.
    function listing_expiry_date() {
    
    	// Get the current date
    	date_default_timezone_set('Europe/London');
    	$today = date('m/d/Y');
    							
    	// 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 the listing ID
    			$post_id = get_the_ID();
    				
    	// Get listing post published date (not pending date)	 
    	//$published_date = get_the_date( 'm/d/Y', get_the_ID() );
    	$published_date = date('m/d/Y');
    													
            $expiry_date  = date( 'm/d/Y', strtotime( '+3 days', strtotime($published_date) ) );		
    			
            // If todays date is equal to or greater than the expiry date...
            if ( ($today == $expiry_date) || ($today > $expiry_date) ) :
    
    	// change post status to 'expired'
    	$postdata = array(
    	'ID'          => $post_id,
    	'post_status' => 'expired',
    
            );
    
    	// Update post data
    	wp_update_post($postdata);
    																				
    	endif;							
    			
    	endwhile; //endwhile The Loop
    endif; //endif The Loop			
    	
    }
  • I do not completely understand your code.

    But this is setting the publish date to the current date

    
    $published_date = date('m/d/Y');
    

    also, because you are using ‘m/d/y’ which could give false results.

    You also need to include the statement global $post in your function if you are going to use $query->the_post();

    
    function listing_expiry_date() {
      global $post; // important
      
      // your query and loop
      
      // date calc
      $published_date = get_the_date('Y-m-d');
      $expire = date('Y-m-d', strtotime($published_date.' +3 days'));
      $today = date('Y-m-d');
      if ($today >= $expire) {
        // expire post
      }
    }
    
  • Could you not condense the query down to something like:

    function listing_expiry_date() {	
    
    	$args = array(
    		'post_type' => array('post_type_listings'),
    		'posts_per_page' => -1,
    		'post_status' => 'publish',
    		'orderby' => 'post_date',
    		'order' => 'DESC',
    
    		'date_query' => array(
    			array(
    				#'after'     => 'January 1st, 2013',
    				'before'    => array(
    					'year'  => date("Y"),
    					'month' => date('m'),
    					'day'   => date('d'),
    				),
    				'inclusive' => true,
    			),
    		),
      
    		
    	);
    	$the_query = new WP_Query( $args );
    	if ( $the_query->have_posts() ) {
    		while ( $the_query->have_posts() ) {
    
    			$the_query->the_post();
    
    			# change the post status
    			$postdata = array(
    				'ID' => $post->ID,
    				'post_status' => 'expired'
    			);	
    			wp_update_post($postdata);								
    
    		}
    	wp_reset_postdata();
    	}
    }

    Code untested!

  • 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()
  • I’ve even hard coded the expiry date and it’s still not working… weird.

    $expiry_date = '2022-01-21';

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

You must be logged in to reply to this topic.