Last active
September 26, 2019 16:35
-
-
Save dashkevych/3d0f4518ee2ed21f7f23a9ae2fb8de94 to your computer and use it in GitHub Desktop.
TBN: caption transfer
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
function tbn_update_featured_image_captions() { | |
$caption_counter = 1; | |
$is_done = get_transient( '_breakingnews_caption_transfer' ); | |
if ( $is_done ) { | |
echo '<h2>==> The update process is already done <==</h2>'; | |
return; | |
} | |
for ($page = 1; $page <= 3; $page++) { | |
$args = array( | |
'post_type' => 'post', | |
'meta_key' => '_td_featured_image_credits', | |
'posts_per_page' => 1000, | |
'paged' => $page, | |
); | |
$custom_query = new WP_Query( $args ); | |
while ( $custom_query->have_posts() ) : $custom_query->the_post(); | |
if ( has_post_thumbnail() && get_post_thumbnail_id( get_the_ID() ) ) { | |
$old_caption = get_post_meta( get_the_ID(), '_td_featured_image_credits', true ); | |
wp_update_post( | |
array( | |
'ID' => get_post_thumbnail_id( get_the_ID() ), | |
'post_excerpt' => $old_caption, | |
) | |
); | |
echo $caption_counter . '--> "'. $old_caption .'" has been updated. <br>'; | |
$caption_counter++; | |
} | |
endwhile; | |
wp_reset_postdata(); | |
} | |
set_transient( '_breakingnews_caption_transfer', true, 14400 ); | |
} | |
add_action( 'wp_footer', 'tbn_update_featured_image_captions' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment