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()
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?
— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.