Skip to content

Instantly share code, notes, and snippets.

@everaldomatias
Last active August 29, 2015 14:04
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 everaldomatias/4c674a7314a06308f1bb to your computer and use it in GitHub Desktop.
Save everaldomatias/4c674a7314a06308f1bb to your computer and use it in GitHub Desktop.
<?php
// Versão funcionando, pelo menos para minha necessidade
// Essa função é disparada com alguns critérios em uma página usada apenas para essa migração.
function migration() {
include( 'wp-admin/includes/media.php' );
include( 'wp-admin/includes/file.php' );
include( 'wp-admin/includes/image.php' );
$query = new WP_Query( array(
'post_type' => 'post', /* O post type que vai usar*/
'posts_per_page' => -1,
'meta_query' => array(
array(
'key' => 'mgr_image',
),
),
) );
if ( $query ) {
while ( $query->have_posts() ) :
$query->the_post();
$title = get_the_title();
$i = get_campoPersonalizado( 'mgr_imagem' );
$img = trim( $i );
$_id = get_the_ID(); /* Esse é o ID do post */
if ( ! empty( $img ) ) {
$path = explode( '.', $img );
$url = "http://www.antigosite.com.br/uploads/" . $path[0] . "/" . $img;
$filename = download_url( $url );
$filetype = wp_check_filetype( basename( $filename ), null );
$wp_upload_dir = wp_upload_dir();
$tmp = download_url( $url );
$file_array = array(
'name' => basename( $url ),
'tmp_name' => $tmp
);
if ( is_wp_error( $tmp ) ) {
@unlink( $file_array[ 'tmp_name' ] );
return $tmp;
}
$id = media_handle_sideload( $file_array, $_id ); /* Esse é o ID do attachment */
if ( is_wp_error( $id ) ) {
@unlink( $file_array['tmp_name'] );
return $id;
}
set_post_thumbnail( $_id, $id );
}
endwhile;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment