WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm using a photoblog theme which upon creating a new post it takes the post attachment and saves a modified "img src" string into the excerpt field. It then uses

<?php the_excerpt(); ?> 

to display the thumbnail on pages like homepage menu, category pages and tag archives.

My website has a few hundred photos on it, and the theme isn't retroactively creating the excerpt-based thumbnails for the old posts.

The developer is no longer officially supporting this theme, and after rifling through their support forum section, they even admitted "unfortunately, i don’t know how to create old thumbnails."-- citation: http://everydays.hassii.com/archives/2541/comment-page-1#comment-9381

Here's the bit of code they are using upon creating this excerpt thumbnail information on new/updated posts.

// ===== PB AUTO-INSERT EXCERPT ===== //
function pb_insert_excerpt(){
    $post_data = &$_POST;
    $post_id = $post_data['ID'] ;
    $post_title = $post_data['post_title'];
    $post_excerpt = $post_data['post_excerpt'];
    $existing_img = strstr($post_excerpt, 'jpg');
    if($post_data['post_excerpt'] = isset($post_data['excerpt'])) {
            if ($existing_img) {
                return $post_excerpt;
        } else {
                $arrImages =& get_children('post_type=attachment&post_mime_type=image&post_parent=' . $post_id); 
                if($arrImages) {
                    $arrKeys = array_keys($arrImages); 
                    $iNum = $arrKeys[0];
                    $sThumbUrl = wp_get_attachment_thumb_url($iNum); 
                    $thumbWidth = get_option("thumbnail_size_w");
                    $thumbHeight = get_option("thumbnail_size_h");
                    $sImgString = '<img src="' . $sThumbUrl . '" width="'.$thumbWidth.'" height="'.$thumbHeight.'" alt="'.$post_title.'" title="'.$post_title.'" />' ;          
                    return $sImgString;

add_filter('excerpt_save_pre', 'pb_insert_excerpt');

Using this same logic, is it possible to scan all old posts and create the excerpt img src thumbnail section?

share|improve this question
up vote 1 down vote accepted

Issue 1: creating the excerpt data

This should be fairly easy to do directly with SQL.

UPDATE         wp_posts AS post
    INNER JOIN wp_posts AS attachment
        ON     attachment.post_type = 'attachment' AND
               post.ID = attachment.post_parent
    SET        post.post_excerpt = CONCAT('<img src="', attachment.guid,'" />')
    WHERE      post.post_excerpt = '';

This is obviously a "one-time fix" that implies you have access to the database.

If the plug-in allows for more than one picture on one post and is using cover pictures to identify which of the pictures in the post's gallery is the "default" used in the excerpt, then we need to extend the join clauses to check in wp_postmeta if the attachment is a cover picture or not.

Issue 2: updating the urls in the excerpt (from guid to thumbnail)

UPDATE         wp_posts AS post
    INNER JOIN wp_posts AS attachment
        ON     attachment.post_type = 'attachment' AND
               post.ID = attachment.post_parent
    SET        post.post_excerpt = CONCAT('<img src="', REPLACE(attachment.guid, '.jpg', '-100x100.jpg'),'" />')
    WHERE      post.post_excerpt = CONCAT('<img src="', attachment.guid,'" />');

This should work only for JPG files, and it will apply the suffix of -100x100, replace in the suffix whatever dimensions you're using. Provided that your dimensions are the same for all files.

If not we need a php script to read the postmeta data to accomplish this task.

share|improve this answer
This worked great, but it's getting the main attachment image, and not the thumbnail. So when it goes to resize it, it looks weird. Anyway to grab the thumbnail URL? – Pat May 28 '12 at 3:11
The thumbnail URL is in a serialised wp_postmeta record, we'll need PHP for that, I can update my answer in 4-5 hours. – Mihai Stancu May 28 '12 at 3:57

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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