WordPress.org

Ready to get started?Download WordPress

Forums

Auto Post Thumbnail
FIX: (if you've deleted thumbnails in past) (4 posts)

  1. eliddon
    Member
    Posted 1 year ago #

    I had an issue where not all posts were getting images. I finally figured out since in the past I had removed the thumbnails (so that I could regenerate them to be a larger size), the Inside the image tag the class="wp-image-####"> no longer referenced an image in the database. The way I was able to fix this is in auto-post-thumbnail.php after approx LINE 263 (After This Code):

    preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
    $thumb_id = $thumb_id[1];

    Add the following:

    /* Check if $thumb_id actually exists in DB */
    if ($thumb_id) {
      $result = $wpdb->get_results("Select ID FROM {$wpdb->posts} WHERE ID = '".$thumb_id."'");
      if (!$result)
        unset($thumb_id);
    }

    This will allow Auto Post Thumbnail to regenerate the image in case your DB is screwed up from deleting previous thumbnails. In addition I'm going to look at setting the correct wp-image-### for the images in the content of each post, but have yet to figure that out. If anyone has some suggestions?

    Hopefully this helps someone else out, possibly we can get it added into the code for next release.

    http://wordpress.org/extend/plugins/auto-post-thumbnail/

  2. eliddon
    Member
    Posted 1 year ago #

    To fix the wp-image tags we can do the following (Note, this will only fix the ones for the image that Auto Post Thumbnail creates) Also this includes the fix above.

    After (around line 263)

    preg_match('/wp-image-([\d]*)/i', $image, $thumb_id);
    $thumb_id = $thumb_id[1];

    Add:

    $old_thumb_id = $thumb_id;
    
    * Check if $thumb_id actually exists in DB */
    if ($thumb_id) {
      $result = $wpdb->get_results("Select ID FROM {$wpdb->posts} WHERE ID = '".$thumb_id."'");
      if (!$result)
        unset($thumb_id);
    }

    Then After (around line 287)

    if ($thumb_id) {
      update_post_meta( $post_id, '_thumbnail_id', $thumb_id );

    Add:

    /* Fix Image class tag in Post wp-image-#### */
    if ($old_thumb_id) {
      $updated_post = array();
      $updated_post['ID'] = $post_id;
      $updated_post['post_content'] = preg_replace('/wp-image-'.$old_thumb_id.'/i','wp-image-'.$thumb_id,$post[0]->post_content);
      wp_update_post( $updated_post );
    }

    Just to sum up, I ran into this problem because I wanted to resize all my thumbnails larger (and the regenerate thumbnails plugin didn't reset the featured image).

    So I ran the followign code (from a .php file in the root)

    require_once("wp-load.php");
    echo 'Working... '; ob_flush();
    global $wpdb;
    $attachments = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_thumbnail_id'");
    foreach($attachments as $attachment){
         $wpdb->query("DELETE FROM $wpdb->postmeta WHERE meta_id = '$attachment->meta_id' LIMIT 1");
         wp_delete_attachment($attachment->meta_value, true);
    }
    echo 'done!';

    Code was found from Here

    Note, I don't recommend deleting your previous thumbnails as it will effect your SEO on your images since essentially they are new images. Although I'm not entirely sure how you would increase your thumbnail size without impacting your SEO on the images.

    Hopefully this helps someone!

  3. natostanco
    Member
    Posted 1 year ago #

    thank you! this is what I needed, In my case syndicating RSS feeds which had img tags with wp-image which were related the the blog of the feed and not mine therefore not generating thumbnails
    this should be added in a new update :)
    there is also a plugin that has a batch command to delete all the thumbnails of the post if you do not want to create the small php file!

  4. eliddon
    Member
    Posted 1 year ago #

    It is not letting me edit my earlier post, but just wanted to forewarn people Do not run that code to Delete the featured image. Using the wp_delete_attachment will also cause it to delete the First Image of every post. If a moderator sees this maybe they can remove that part from above.

    I didn't notice the issue because when I re-ran Auto Post Thumbnail from my dev site it was using my production site to grab the Thumbnails and would recreate all the images properly. When I ran it on production... not so lucky :)

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic