Skip to content

Instantly share code, notes, and snippets.

@mwordpress
Created May 13, 2017 09:43
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 mwordpress/e9e8160e36e57fef62544cdc536fc2c6 to your computer and use it in GitHub Desktop.
Save mwordpress/e9e8160e36e57fef62544cdc536fc2c6 to your computer and use it in GitHub Desktop.
add custom field to all post based on post thumbnail
<?php
include '../../../wp-load.php';
$args = array(
'posts_per_page' => -1,
'post_type' => 'post',
'post_status' => 'publish',
'suppress_filters' => true,
);
$posts_array = get_posts( $args );
foreach($posts_array as $post_array) {
if ( has_post_thumbnail($post_array) ) :
$image_key_exists = metadata_exists( 'post', $post_array->ID, 'mwp_thumbnail_url' );
$image = wp_get_attachment_image_src( get_post_thumbnail_id($post_array->ID), 'full' );
$url = $image['0'];
if($image_key_exists == true && !empty($image_key_exists) ) {
update_post_meta($post_array->ID, 'mwp_thumbnail_url', $url, true );
} else {
add_post_meta($post_array->ID, 'mwp_thumbnail_url', $url, true );
}
endif;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment