Skip to content

Instantly share code, notes, and snippets.

@titodevera
Last active June 22, 2017 12:29
Show Gist options
  • Save titodevera/dbb979286408182e748b080a190805c5 to your computer and use it in GitHub Desktop.
Save titodevera/dbb979286408182e748b080a190805c5 to your computer and use it in GitHub Desktop.
Update existing posts to random publication dates
$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