Last active
June 22, 2017 12:29
-
-
Save titodevera/dbb979286408182e748b080a190805c5 to your computer and use it in GitHub Desktop.
Update existing posts to random publication dates
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$posts = get_posts( | |
array( | |
'numberposts' => -1, | |
'post_status' => 'any', | |
'post_type' => 'post' //you can use a cpt | |
) | |
); | |
foreach( $posts as $post ) { | |
//Generate a random date between 1 May 2017 and now | |
$random_date = mt_rand( strtotime( '1 May 2017' ), time() ); | |
$date_format = 'Y-m-d H:i:s'; | |
$post_date = date( $date_format, $random_date ); | |
//Update the post | |
$update = array( | |
'ID' => $post->ID, | |
'post_date' => $post_date, | |
'post_date_gmt' => null, | |
); | |
wp_update_post( $update ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment