Skip to content

Instantly share code, notes, and snippets.

@scottopolis
Created February 4, 2019 22:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottopolis/f0a8954179dc4699018274b2cc1efda4 to your computer and use it in GitHub Desktop.
Save scottopolis/f0a8954179dc4699018274b2cc1efda4 to your computer and use it in GitHub Desktop.
Add media image to posts that have a media url
<?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