Scheduling wp cron events is especially useful for website owners managing multiple blog posts written by guest authors or announcements, such as flash sales, limited-time offers, or event updates. Instead of manually updating or unpublishing each post, you can rely on this cron schedule event to publish or unpublish the post automatically.
In this guide, we’ll show you how to automatically change the post status from Published to Pending using a one-time WP-Cron event in WordPress.
Solution: WP-Cron Event in WordPress
// Schedule the cron event if it's not already scheduled function ts_schedule_post_pending() { if (!wp_next_scheduled('ts_change_post_status_pending')) { wp_schedule_single_event(time() + 5, 'ts_change_post_status_pending'); } } add_action('init', 'ts_schedule_post_pending'); // Hook function to change post status to 'pending' function ts_change_post_status() { // Get posts that need to be updated $posts = get_posts(array( 'post_type' => 'post', // Change this if needed 'post_status' => 'publish', // Only change published posts 'numberposts' => -1, // Get all matching posts )); // Update post status foreach ($posts as $post) { wp_update_post(array( 'ID' => $post->ID, 'post_status' => 'pending', )); } } add_action('ts_change_post_status_pending', 'ts_change_post_status');