0

I'm changing the theme of a blog. The problem is when I export the content from the old post and import it into the new one, I don't get the Featured Image.

It's the same problem posted here:

Move from old custom field to new post_thumbnails

Only that I couldn't get that solution to work for me… I'm kind of a noob.

The original blog uses a meta key of "mainimg_medium" for the "featured image", and the new blog uses "_thumbnail_id".

Any help would be greatly appreciated, Thanks!

  • what exactly do not work using solution in answer you linked? – gmazzap Jul 25 '13 at 3:58
1

I put this together for you as a proof-of-concept. This class will migrate both attachment IDs and external image URLs into your featured images.

class Featured_Image_Migration{

    #Instantiate the migration by hitting /wp-admin/?migrate-post-meta=foobar
    protected $key = 'migrate-post-meta';
    protected $secret = 'foobar';

    #This is your legacy meta_key
    protected $meta_key = 'mainimg_medium';

    function __construct(){
        #Force this to run last so all includes are available
        add_action( 'admin_init', array($this, 'migrate_post_meta'), 999999 );
    }

    public function migrate_post_meta(){

        #Verify authorization and authentication
        if ( ! $this->verify_request() )
            return;

        global $wpdb;

        #Retrieve all featured image postmeta rows from prior theme
        $query = $wpdb->prepare( "SELECT post_id, meta_value FROM $wpdb->postmeta WHERE meta_key = %s", $this->meta_ );
        $results = $wpdb->get_results( $query );

        if ( $results ){
            foreach( $results as $result ){

                #Check if meta_value is a path
                if( preg_match( '/\//', $result->meta_value ) ){
                    $this->get_attachment_by_url( $result->meta_value, $result->post_id );
                    continue;
                }

                #Check if meta_value is an ID
                if( is_int( (int)$result->meta_value ) )

                    #Add a native WordPress featured image postmeta row
                    if( update_post_meta( $result->post_id, '_thumbnail_id', $result->meta_value ) )

                        #Send a message to the screen
                        show_message( "Post #$result->post_id - Featured image was set properly." );
            }
        }

        #Kill the admin process to see the message log
        exit;
    }

    private function verify_request(){

        #Only admins and those with the key/secret can run this in wp-admin
        if( current_user_can( 'manage_options' ) && isset( $_GET[$key] ) && $_GET[$key] === $secret )
            return true;

        return false;
    }

    private function get_attachment_by_url( $url, $post_id ){

        global $wpdb;

        #This is about as close as we can come to finding the attachment
        $query = $wpdb->prepare( "SELECT ID from $wpdb->posts WHERE guid = %s LIMIT 1", $url );
        $id = $wpdb->get_var( $query );

        if($id)
            #We found a match in the database!
            return $id;

        else{
            #If the URL is external, download the image into the post attachments
            add_action( 'add_attachment', array( $this, 'add_attachment') );
            $attachment = media_sideload_image( $url, $post_id );
            remove_action( 'add_attachment', array( $this, 'add_attachment') );
        }
    }

    public function add_attachment($id){

        #Get attachment post object
        $attachment = get_post( $id );

        #Add our newly sideloaded attachment as the featured image
        if( update_post_meta( $attachment->post_parent, '_thumbnail_id', $attachment->ID ) )
            show_message( "Post #$attachment->post_parent - Featured image was sideloaded and set properly." );
    }

}
new Featured_Image_Migration;
  • Hi Brian, thank you so much for your time. Complete noob here… I should add this to my functions.php, right? As little as I understand of php, this seems to do exactly what's needed, but for some reason it doesn't work… Any ideas? What additional information would you need to figure this out? Again, thank you so much for your time! – Nico S Jul 25 '13 at 20:08
  • @NicoS you can add this to your functions.php file or add it as a plugin. You will need to add a plugin header first if you do the latter. codex.wordpress.org/File_Header#Plugin_File_Header_Example – Brian Fegter Jul 25 '13 at 21:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.