Skip to content

Instantly share code, notes, and snippets.

@rilwis
Last active October 6, 2015 13:28
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 rilwis/3001048 to your computer and use it in GitHub Desktop.
Save rilwis/3001048 to your computer and use it in GitHub Desktop.
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