Skip to content

Instantly share code, notes, and snippets.

@rilwis

rilwis/fix-thumbnail.php

Last active Oct 6, 2015
Embed
What would you like to do?
Fix missing thumbnail for Auto Post Thumbnail plugin
<?php
/**
* Usage: upload to WP folder
* Run it!
*/
echo 'Working... ';
require_once( 'wp-load.php' );
global $wpdb;
$atts = $wpdb->get_col("
SELECT DISTINCT ID
FROM {$wpdb->posts}
WHERE post_type = 'attachment'
");
foreach ( $atts as $att )
{
$sql = '';
$check = $wpdb->get_col( "
SELECT meta_id
FROM {$wpdb->postmeta}
WHERE post_id = '{$att}' AND meta_key = '_wp_attached_file'
");
// We've already done this one...
if ( !empty( $check ) )
continue;
$thumb = $wpdb->get_row("
SELECT meta_key, meta_value
FROM {$wpdb->postmeta}
WHERE post_id = '{$att}' AND meta_key = '_wp_attachment_metadata'
");
if ( empty( $thumb ) || !isset( $thumb->meta_value ) )
continue;
$data = unserialize( $thumb->meta_value );
if ( !isset( $data['sizes']['thumbnail']['file'] ) )
continue;
$path = dirname( $data['file'] ) . '/' . $data['sizes']['thumbnail']['file'];
$wpdb->query("
INSERT INTO {$wpdb->postmeta}
SET post_id='{$att}', meta_key='_wp_attached_file', meta_value='{$path}'
");
}
echo 'Done!';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.