-
Notifications
You must be signed in to change notification settings - Fork 0
/
wp-cli-reset-post-date-command.php
53 lines (40 loc) · 1.54 KB
/
wp-cli-reset-post-date-command.php
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php
class WP_CLI_Reset_Post_Date_Command extends WP_CLI_Command {
/**
* 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." );
}
}
WP_CLI::add_command( 'reset-post-date', 'WP_CLI_Reset_Post_Date_Command' );