I have 1700 Motivational Updates + Write ups imported to WordPress.
The problem is they are all published on the same date.
I'd ideally like for them to be published once per day.
I researched a lot and found this.
$date1 = date( $date_format, strtotime("-1 year", time()) );
$date2 = date( $date_format, time() );
$post_type = 'post'; // post, page, product or some other custom post type
// Get all the posts
$posts = get_posts( array(
'numberposts' => -1, // get ALL posts
'post_status' => 'any', // published, pending, draft, future, trash
'post_type' => $post_type
));
foreach( $posts as $post ) {
//* Generate a random post dates
$random_date = mt_rand( strtotime( $date1 ), strtotime($date2) );
//* Format the date that WordPress likes
$post_date = date( $date_format, $random_date );
// We only want to update the post date
$update = array(
'ID' => $post->ID,
'post_date' => $post_date,
'post_date_gmt' => null,
);
//* Update the post
wp_update_post( $update );
}
This setup works great, if am trying to randomize the post dates. However, it sometimes sets 2 or even 5 posts in the same day.
Is there someway to have it such that the published_date is set to 1 per day, the time of the day can be randomized.
Any help is greatly appreciated.