The check_and_publish_future_post() is the WordPress builtin function to clean the post in the cache. This function not run if $_wp_suspend_cache_invalidation is not empty.
Cleaning means delete from the cache of the post. Will call to clean the term object cache associated with the post ID.
Syntax:
1 | clean_post_cache( int|WP_Post $post ) |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 | /** * Reset the post_date field on your posts. * A sadly necessary step after you change your timezone in WordPress * * @synopsis --post_type=<post-type> */ public function __invoke($args, $assoc_args) { global $wpdb; $query_args = array('post_type' => $assoc_args['post_type'], 'posts_per_page' => -1, 'post_status' => 'publish'); $query = new WP_Query($query_args); if (empty($query->posts)) { WP_CLI::error("No posts found"); } WP_CLI::line(sprintf("Updating post_date on %d posts.", count($query->posts))); foreach ($query->posts as $key => $post) { if (empty($post->post_date_gmt) || "0000-00-00 00:00:00" == $post->post_date_gmt) { WP_CLI::line(sprintf("Error: Post %d is missing a publish date.", $post->ID)); continue; } $original = $post->post_date; $new = get_date_from_gmt($post->post_date_gmt); if ($new == $original) { WP_CLI::line(sprintf("No Change: Post %d has the correct post_date of %s", $post->ID, $original)); continue; } $wpdb->update($wpdb->posts, array('post_date' => $new), array('ID' => $post->ID)); clean_post_cache($post->ID); WP_CLI::line(sprintf("Updated: Post %d changed from %s to %s", $post->ID, $original, $new)); if ($key && $key % 10 == 0) { sleep(1); } } WP_CLI::success("Posts were updated with the correct post_date."); } |
1 2 3 4 5 6 7 8 9 10 | public function update(array $post_data_array, $direct_db_update = false) { if ($direct_db_update) { $this->wpdb->update($this->wpdb->posts, $post_data_array, array('ID' => $this->post_id)); clean_post_cache($this->post_id); } else { $post_data_array['ID'] = $this->post_id; wp_update_post($post_data_array); } } |
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.