Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hey I'm a WP noob but I was given the task of upgrading WP from 3.2.x to 3.5.x. Now some images aren't showing.

Here's the chunk of code responsible for it:

$query = "select w1.guid,w2.post_title, w1.ID from wp_posts w1,wp_posts w2 where w2.id=w1.post_parent and w1.post_type='attachment' and w1.post_parent in (".$post_a.") and w2.post_status='publish' ORDER BY w2.post_date DESC ".$lim_." ";

$terms = $wpdb->get_results($query);
print_r($terms); // for debugging
for ($i = 0; $i < sizeof($terms); $i++) {
    $gallery_image_thumb = wp_get_attachment_image_src($terms[$i]->ID, 'thumbnail');

    if ($size != "") {
        $result_[$i] = '<img src="'.$gallery_image_thumb[0].'" alt="'.$terms[$i]->post_title.'" border="3"> ';
    } else {
        $result_[$i] = '<img src="'.$gallery_image_thumb[0].'" alt="'.$terms[$i]->post_title.'" border="0">';
    }

So for one section it shows images properly, but for another it generates images with empty srcs.

So I printed out the $terms search results array and this is what comes up for the section that works:

Array ( [0] => stdClass Object ( [guid] => http://www.mysite.com/wp-content/uploads/2013/05/jmc-body.jpg [post_title] => jmc-body [ID] => 28358 ) [1] => stdClass Object ( [guid] => http://www.mysite.com/wp-content/uploads/2013/05/jmc-80.jpg [post_title] => jmc-80 [ID] => 28356 )

while this is the $terms array for the section that doesn't work:

Array ( [0] => stdClass Object ( [guid] => http://www.mysite.com/images/mysite/gallery_latest_images/ckc721.jpg [post_title] => CKC_704 [ID] => 25367 ) [1] => stdClass Object ( [guid] => http://www.mysite.com/images/mysite/gallery_latest_images/jmc1294toy.jpg [post_title] => JMC_1294 [ID] => 25366 )

So I'm guessing that I need to add an if/else condition on this line: $gallery_image_thumb = wp_get_attachment_image_src($terms[$i]->ID, 'thumbnail'); ? I assume wp_get_attachment_image_src looks only in uploads? Does anyone know a clean way of fixing this? I'm not sure what kind of if/else statement I could do... unless it were a regex for matching the guid?

share|improve this question
    
What should be the correct URL(s) for the broken images? –  s_ha_dum Jul 18 '13 at 16:14
    
The guids listed above all have the right URLs, but wp_get_attachment_image_src($terms[$i]->ID, 'thumbnail'); doesn't seem to work on the second array for some reason. –  Edmund Jul 18 '13 at 16:19

1 Answer 1

I don't have time to produce working code for this. I will edit the answer if I get the chance.

In the meantime, you probably want a filter on upload_dir to alter that path.

Your code would look something like this then:

function alter_upload_wpse_107016($upload) {
  remove_filter('upload_dir','alter_upload_wpse_107016');
  // alter $upload array
  return $upload;
}
if (1 === preg_match('|regex-to-check-guid|',$terms[$i]->guid) {
  add_filter('upload_dir','alter_upload_wpse_107016');
}
$gallery_image_thumb = wp_get_attachment_image_src($terms[$i]->ID, 'thumbnail');

That is completely untested and written entirely from memory. No promises but maybe it will get you started.

By the way, the guid is not an URL, however much it may look like one, and should not be used as such (even though it was used as such by WordPress prior to something like 2.7).

share|improve this answer
    
what does $upload stand for/ –  Edmund Jul 18 '13 at 18:58
    
That is the information passed into the filter by WordPress. var_dump it or look at the source I linked to-- second parameter of apply_filters. –  s_ha_dum Jul 18 '13 at 19:02
    
I'm not sure how to alter $upload array as you wrote above. What would I do there? I copied and pasted your code and it didn't change –  Edmund Jul 18 '13 at 19:08
    
using $terms[$i]->guid like this: '<img src="'.$terms[$i]->guid.'" alt="'.$terms[$i]->post_title.'" border="3"> ' worked... but you said it's bad to do right? –  Edmund Jul 18 '13 at 19:21
    
It is not a good long-term solution. You really need to get the images migrated or get filters in place to load from that directory correctly. –  s_ha_dum Jul 18 '13 at 19:43

Your Answer

 
discard

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.