PHP WordPress Code To Bulk Edit Date Of Posts
Do you want to set your posts to a specific date? Or perhaps backdate a large number of post? If you want to do either, to do so manually will be very time consuming. Hence, I came up with a quick snippet that can be used to help you achieve this.
$posts = get_posts( array( 'numberposts' => -1, 'post_status' => 'any', ) ); foreach ($posts as $post) { $post_ID = $post->ID; $post_date = get_the_date('', $post_ID); // default option is Y-m-d $post_date = date('Y-m-d', strtotime('-1 month', strtotime($post_date))); // set the date here e.g $post_date='2019-07-01', or just subtract from the existing post date, which is what this code is doing at the moment. $post_data = array( 'ID' => $post_ID, 'post_date' => $post_date ); //wp_insert_post( $post_data ); wp_update_post( $post_data ); }
In the code above, just set the $posts
variable to whatever posts you want to modify or edit. Currently, it gets ALL of your posts. You can set the arguments accordingly to filter to whatever you wish.
The other thing you might want to change is the $post_date
variable. You can either change it to a fixed date like so, $post_date = '2019-07-01'
, or subtract or add a specific number of day/month/year to the existing date of the post (which is currently what the code does)
Let me know if you need any assistance in the comments below. I will be glad to help.
Be First to Comment