Created
February 4, 2019 22:40
-
-
Save scottopolis/f0a8954179dc4699018274b2cc1efda4 to your computer and use it in GitHub Desktop.
Add media image to posts that have a media url
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 | |
// add this code to a plugin, it only needs to run once so remove it when you're done | |
add_action('admin_init', function() { | |
$the_query = new WP_Query( array( 'post_type' => 'post', 'posts_per_page' => 999 ) ); | |
// The Loop | |
if ( $the_query->have_posts() ) { | |
while ( $the_query->have_posts() ) { | |
$the_query->the_post(); | |
// save meta field for the app | |
if( get_post_meta( $the_query->post->ID, 'appp_media_url', 1 ) ) { | |
// optionally add an image when the media is played | |
$featured_url = wp_get_attachment_url( get_post_thumbnail_id( $the_query->post->ID ) ); | |
update_post_meta( $the_query->post->ID, 'appp_media_image', $featured_url ); | |
} | |
} | |
/* Restore original Post Data */ | |
wp_reset_postdata(); | |
} else { | |
// no posts found | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment