Skip to content

Instantly share code, notes, and snippets.

@hearvox
Created July 19, 2018 16:45
Show Gist options
  • Save hearvox/b3678fdd0b48077b83176936d7065566 to your computer and use it in GitHub Desktop.
Save hearvox/b3678fdd0b48077b83176936d7065566 to your computer and use it in GitHub Desktop.
WordPress: update post dates based on post meta value (used for radio series to change dates from rerun airdate to original airdate)
<?php
// Get category posts.
$query_args = array(
'category_name' => 'episode', // Name of category.
'posts_per_page' => 50, // Do in bathces to avoid overloading server.
'offset' => 0, // Skip previous batch by incrementing by 50 each time run.
);
$query = new WP_Query( $query_args );
// Update post dates and write redirect directive for htaccess file.
foreach ( $query->posts as $post ) {
$new_date = get_post_meta( $post->ID, 'airdate2', true ); // Date to use, in format Y-d-m.
if ( $airdate2 ) {
$new_date = $new_date . ' 00:00:00'; // WP date format: Y-d-m H:i:s
$new_date_gmt = get_gmt_from_date( $post_date );
// Write redirect directive with old URL.
echo 'RedirectPermanent ' . get_permalink( $post->ID ) . ' ';
$args_update = array (
'ID' => $post->ID,
'post_date' => $new_date,
'post_date_gmt' => $new_date_gmt,
);
wp_update_post( $args_update ); // Set new post date.
// Add new URL to directive.
echo get_permalink( $post->ID ) . '<br>';
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment