Last active
October 6, 2015 13:28
-
-
Save rilwis/3001048 to your computer and use it in GitHub Desktop.
Fix missing thumbnail for Auto Post Thumbnail plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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