【发布时间】:2022-01-20 22:59:05
【问题描述】:
我的代码功能有问题。自定义帖子应在一定天数后过期。目前设置为 3 天,以便我进行测试。
但是即使过期日期早于今天的日期,帖子也会过期,我不知道为什么?!
// 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() );
$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
}
【问题讨论】: