Last active
August 29, 2015 14:07
-
-
Save caercam/fab579c61eecb8d32d88 to your computer and use it in GitHub Desktop.
Update all movie titles to include movie date.
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
<?php | |
/** | |
* Update all movie titles to include movie date. | |
* | |
* @package WordPress | |
*/ | |
require( dirname( __FILE__ ) . '/wp-blog-header.php' ); | |
$query = new WP_Query( | |
array( | |
'post_type' => 'movie', | |
'posts_per_page' => -1 | |
) | |
); | |
if ( $query->have_posts() ) : | |
while ( $query->have_posts() ) : | |
$query->the_post(); | |
$meta = get_post_meta( $post->ID, '_wpml_movie_data', $single = true ); | |
if ( isset( $meta['meta']['release_date'] ) && '' != $meta['meta']['release_date'] ) : | |
$date = $meta['meta']['release_date']; | |
$date = date_i18n( 'Y', strtotime( $date ) ); | |
// Make sure this movie hasn't been updated yet | |
if ( $date != substr( $post->post_title, -4 ) ) : | |
$post->post_title = sprintf( '%s (%d)', $post->post_title, $date ); | |
$update = wp_update_post( $post, $wp_error = true ); | |
if ( is_wp_error( $update ) ) | |
printf( '"%s": %s<br />', $post->post_title, $update->get_error_message() ); | |
else | |
printf( '"%s" update successfully!<br />', $post->post_title ); | |
endif; | |
endif; | |
endwhile; | |
endif; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment