我没有离开任何expired
发布状态,但您可以将发布状态设置为draft
属于trash
.
首先,您需要设置一个cron作业以每隔一段时间运行一次,并检查日期是否已到。找到合适的时间取决于你。
// Let\'s check the scheduled even on WP load
add_action( \'init\', \'schedule_post_expiration\' );
function schedule_post_expiration() {
// If the even is not scheduled already, then do schedule it
if ( ! wp_next_scheduled( \'reset-post-status\' ) ) {
wp_schedule_event( strtotime( \'00:00:00\' ), \'daily\', \'reset-post-status\' );
}
}
// We hook our callback function to our custom action hook to run
// daily and update the post\'s status
add_action( \'reset-post-status\', \'reset_post_status_callback\' );
function reset_post_status_callback(){
// Get the current date
$date = date( \'Y-m-d\' );
// Set the date we want to check. It should match the above date format.
$check_date = \'2018-02-11\';
// Check if the time has come
if ( strtotime( $date ) > strtotime( $check_date ) ){
// Query a list of a specific author\'s posts
$author_args = array( \'author\' => 123, \'post_status\' => \'publish\' );
// Do the query
$author_query = new WP_Query( $author_args );
// Check if the query has any post, and setup the post data
if ( $author_query->have_posts() ) {
while ( $author_query->have_posts() {
$author_query->the_post();
// Update the post\'s status
wp_update_post( array( \'ID\' => get_the_ID(), \'post_status\' => \'trash\' ) );
}
}
// Reset the posts data
wp_reset_postdata();
}
}