Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Moving website - custom postmeta uploaded files not showing WordPress

  • SOLVED

I built a client's website on my dev server, moved it to their hosting account today and the images uploaded through the custom meta fields I created in their theme are no longer showing. No URL is returned in the image src

- I ran an SQL query to replace the filepaths and URLs in the database, and the filepaths and URLs are correct in the database
- File permissions are the same - the hosting account is on the same host
- Images attached to posts display fine

Any thoughts?!

Answers (5)

2012-05-13

John Cotton answers:

Presumably you've copied the images over?

Do the URLs in the meta fields correctly display the images when used directly in a browser?


Dan Davies comments:

Yep, images copied over

If I take the URL from the table row then I and load it in my browser then the image loads - so the paths and URLs are definitely correct.

The website isn't live currently, I've just modified my hosts file, but I can't see that being an issue considering the images load directly.


John Cotton comments:

<blockquote>The website isn't live currently, I've just modified my hosts file, but I can't see that being an issue considering the images load directly.</blockquote>
That seems fair enough.

What about the HTML output - how wrong is it? ie is the img element there without a src, or is the img element missing?

Perhaps if you put up some of the code around where things go wrong we could spot something...


Dan Davies comments:

There's literally no src being output on any of the custom meta images

<img class="active" src="" width="91" height="72" />

All of the standard images inserted into a post through the WordPress media uploader are displaying fine inside the content.


John Cotton comments:

Can you post the code from the point it retrieves the meta value to the output so that I can see what's happening?


Dan Davies comments:

Here's the code in the template


<?php $logos = new WP_Query('post_type=projects&showposts=9&meta_key=bs_feature_clients&meta_value=on'); ?>
<?php if($logos->have_posts()) : ?><?php $counter = 1; while($logos->have_posts()) : $logos->the_post(); ?>
<?php $alogo = get_post_meta($post->ID, 'bs_project_logo', true); ?>
<?php $hlogo = get_post_meta($post->ID, 'bs_project_logo_hover', true); ?>
<?php $class = ''; ?>
<?php if($counter %3) { $class = ''; } else { $class = 'third'; } ?>
<div class="logo <?= $class; ?>">
<a href="<?php the_permalink(); ?>"><img class="active" src="<?php echo $alogo['url']; ?>" width="91" height="72" /></a>
<a href="<?php the_permalink(); ?>"><img class="hover" src="<?php echo $hlogo['url']; ?>" width="91" height="72" /></a>
</div>

<?php $counter++; endwhile; endif; ?>


Here's the output:


<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/pye/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/pye/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/robe/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/robe/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo third">
<a href="http://www.brightsparkcreative.com/projects/pro-health-store/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/pro-health-store/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/pageant-media/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/pageant-media/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/zafferano/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/zafferano/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo third">
<a href="http://www.brightsparkcreative.com/projects/haultastic/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/haultastic/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/jo-behari/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/jo-behari/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/the-goodlife-centre/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/the-goodlife-centre/"><img class="hover" src="" width="91" height="72" /></a>
</div>

<div class="logo third">
<a href="http://www.brightsparkcreative.com/projects/mind/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/mind/"><img class="hover" src="" width="91" height="72" /></a>
</div>


Exact same code as on the dev server, only there the images display!


John Cotton comments:

Just after this line:

<?php $alogo = get_post_meta...

Can you add this and tell me what you get?

<?php print_r($alogo); ?>


Dan Davies comments:

Nothing's different


John Cotton comments:

<blockquote>Nothing's different</blockquote>
I assume you mean nothing is shown?

And that explains why no src is output - the meta data is empty.

You said that the values looked correct in the database.

Have you checked that the post_id value in the post_meta table is the same as the id in the wp_post table (or the id in the url when you are editing that post?).

That's the next step.


Dan Davies comments:

Sorry, yes, nothing is shown.

So looking at the first post that is output by that query:


<div class="logo ">
<a href="http://www.brightsparkcreative.com/projects/pye/"><img class="active" src="" width="91" height="72" /></a>
<a href="http://www.brightsparkcreative.com/projects/pye/"><img class="hover" src="" width="91" height="72" /></a>
</div>


That is post ID 162.

The postmeta row for 'bs_project_logo' on post ID 162 is

a:3:{s:4:"file";s:129:"/home/brightsp/public_html/wp-content/uploads/2012/05/PYE-logo-grey1.jpg";s:3:"url";s:92:"http://www.brightsparkcreative.com/wp-content/uploads/2012/05/PYE-logo-grey1.jpg";s:5:"error";s:0:"";}

The filepath and URL for that file are correct


John Cotton comments:

Try adding

global $post;

just before the code you posted.


Dan Davies comments:

Have done - still no image srcs.

Besides, the code is identical on the dev site


John Cotton comments:

<blockquote>I reuploaded one of the files and that file now displays fine - I'd just rather avoid reuploading 100+ files if I can ;)</blockquote>

Since that works you need to compare the before and after values of the postmeta table to see if there's a difference.

Note the state of one post, upload the files again for that post and then compare.

If they are identical, there must be some external value that's changing, either in another table or cached somewhere. I don't know the theme so can't comment what it might be, but that would be where to look.


Dan Davies comments:

Here's a field that hasn't been updated

a:3:{s:4:"file";s:129:"/home/brightsp/public_html/wp-content/uploads/2012/05/PYE-logo-grey1.jpg";s:3:"url";s:92:"http://www.brightsparkcreative.com/wp-content/uploads/2012/05/PYE-logo-grey1.jpg";s:5:"error";s:0:"";}

And here's one that has

a:3:{s:4:"file";s:75:"/home/brightsp/public_html/wp-content/uploads/2012/05/PYE-logo-colour11.jpg";s:3:"url";s:83:"http://www.brightsparkcreative.com/wp-content/uploads/2012/05/PYE-logo-colour11.jpg";s:5:"error";s:0:"";}

I'm not sure what the numbers mean, but otherwise I don't see a difference.

Interestingly in the database now, there are multiple instances of the same meta_key for that post_id with different meta_ids. I'm not sure if when WordPress saves a post it updates the existing meta_key, or adds a new one with a new meta_id, but I now have multiple instances of bs_project_logo for post_id 162.


John Cotton comments:

OK - I think I know what you're problem is.

The numbers in the serialized array are the character lengths of the string.

Clearly, your dev urls are much longer (or at least of a different length) than your production ones.

When PHP tries to deserialize the array from the database, the numbers are wrong because you've updated the urls/paths.

Thus PHP gives you a null value (since it can't do anything with the corrupt data).


What to do about it?

Rather than doing a search and replace in the database, you've got to write a little routine that pulls all the meta values out (ie deseriliazes) replace the urls/paths and then reserialize back into the database.

Pseudo code would be:


$results = $wpdb->get_results("SELECT * FROM wp_postmeta WHERE meta_key = 'bs_project_logo''');

foreach( $results as $result ) {
$result['url'] = str_replace( OLD_URL, NEW_URL $result['url']);
$wpdb->query("UPDATE.....");
}


Dan Davies comments:

Running into a minefield of objects and arrays now. I expect your solution will work, but given my lack of PHP knowledge it may be quicker to reupload the files!

2012-05-13

idt answers:

Please post the code used to display the images.

Thanks,
idt


Dan Davies comments:

See above :)


idt comments:

What happens if you change <?php echo $alogo['url']; ?> and <?php echo $hlogo['url']; ?> to just <?php echo $alogo; ?> and <?php echo $hlogo; ?>


idt comments:

Also, please try adding: global $post; above your code.


Dan Davies comments:

Nothing is output


idt comments:

You already have global $post; in your code, right?


Dan Davies comments:

Yes. Given the code is working on the dev site, that shouldn't be an issue.


idt comments:

Did you happen to copy the database from one server to another? I think this is the same issue as you're experiencing right now: http://stackoverflow.com/questions/9101323/wordpress-loses-ability-to-read-and-store-custom-metabox-data-when-database-is-c


idt comments:

As per that link: <blockquote>I've "fixed" this by deleting the serialized data from the database manually. I was then able to enter it all back in again by hand...</blockquote>


Dan Davies comments:

Thanks for the link :) Hopefully I won't have to go through and remove & replace all of the meta field values, but it's an option.

Curiously, unlike that poster, it's only the file upload meta that isn't working. All of the standard text fields are returning their data just fine.


idt comments:

It seems though that your issue is the same as that one on the link. Your code seems fine as per replies here so it should have worked. Please test what was suggested there, try changing the value of one entry and let's see how it goes. Thanks.


idt comments:

Also the same as this: [[LINK href="http://wordpress.org/support/topic/custom-post-type-and-custom-meta-box-issues"]][[/LINK]]

So try re-saving or editing one the entries.


idt comments:

Oops... link gone: http://wordpress.org/support/topic/custom-post-type-and-custom-meta-box-issues


Dan Davies comments:

Yeah, I tried re-saving, no change.

I reuploaded one of the files and that file now displays fine - I'd just rather avoid reuploading 100+ files if I can ;)


idt comments:

Out of curiosity, what happens when you remove , true from

get_post_meta($post->ID, 'bs_project_logo', true)

get_post_meta($post->ID, 'bs_project_logo_hover', true)
and used $alogo['url']; and $hlogo['url']; in your code?


Dan Davies comments:

That makes no difference


Dan Davies comments:

Sorry, it does make a difference. If I remove the ', true' then the one image that was displaying no longer displays.

2012-05-13

Christianto answers:

Hi,

Could you check if by echoing $post->ID, the correct value appear?
Or you can replace $post->ID with [[LINK href="http://codex.wordpress.org/Function_Reference/get_the_ID"]]get_the_ID()[[/LINK]]

Let me know if this not works..


Dan Davies comments:

Yep, that returns the correct post IDs


Christianto comments:

If with get_post_meta() with correct ID doesn't work, what if we throw all meta value?
print_r(get_post_custom($post->ID));
Can you see your meta value?


Dan Davies comments:

Yep - all of the postmeta attached to each post is returned correctly.


Christianto comments:

If the meta bs_project_logo & bs_project_logo_hover url value exists,
then you can use it for temporary :)

you could try to re-save the post and all custom field to see if it appear by using get_post_meta();
maybe the problem comes from wp_cache_get() that used inside get_post_meta/get_metadata function.


Christianto comments:

If you want to use get_post_custom() instead of get_post_meta(), you can try this..

<?php $logos = new WP_Query('post_type=projects&showposts=9&meta_key=bs_feature_clients&meta_value=on'); ?>
<?php if($logos->have_posts()) : ?><?php $counter = 1; while($logos->have_posts()) : $logos->the_post(); ?>
<?php $all_meta_data = get_post_custom($post->ID);
<?php $alogo = unserialize($all_meta_data['bs_project_logo'][0]); ?>
<?php $hlogo = unserialize($all_meta_data['bs_project_logo_hover'][0]); ?>
<?php $class = ''; ?>
<?php if($counter %3) { $class = ''; } else { $class = 'third'; } ?>
<div class="logo <?= $class; ?>">
<a href="<?php the_permalink(); ?>"><img class="active" src="<?php echo $alogo['url']; ?>" width="91" height="72" /></a>
<a href="<?php the_permalink(); ?>"><img class="hover" src="<?php echo $hlogo['url']; ?>" width="91" height="72" /></a>
</div>
<?php $counter++; endwhile; endif; ?>


hope this help..


Christianto comments:

Duh.. I forgot the closing php tag..
try code below..
<?php $logos = new WP_Query('post_type=projects&showposts=9&meta_key=bs_feature_clients&meta_value=on'); ?>
<?php if($logos->have_posts()) : ?><?php $counter = 1; while($logos->have_posts()) : $logos->the_post(); ?>
<?php $all_meta_data = get_post_custom($post->ID); ?>
<?php $alogo = unserialize($all_meta_data['bs_project_logo'][0]); ?>
<?php $hlogo = unserialize($all_meta_data['bs_project_logo_hover'][0]); ?>
<?php $class = ''; ?>
<?php if($counter %3) { $class = ''; } else { $class = 'third'; } ?>
<div class="logo <?= $class; ?>">
<a href="<?php the_permalink(); ?>"><img class="active" src="<?php echo $alogo['url']; ?>" width="91" height="72" /></a>
<a href="<?php the_permalink(); ?>"><img class="hover" src="<?php echo $hlogo['url']; ?>" width="91" height="72" /></a>
</div>
<?php $counter++; endwhile; endif; ?>


sorry its late night here.. :)


Dan Davies comments:

I just switched to that code, and the images aren't being displayed (except the one I uploaded to test if re-uploading files would work)

2012-05-13

Jatin Soni answers:

Check your permalink structure with the localhost site..


Dan Davies comments:

Permalinks are default on the dev site, /%category%/%postname%/ on the 'live' site. Switched them to default on the 'live' site and the images still didn't display.


Jatin Soni comments:

Have you use absolute url for custom post meta images?


Dan Davies comments:

Sorry, not sure I understand your question


Jatin Soni comments:

Can you please check and confirm that for further investigation.

2012-05-13

Just Me answers:

When you edit the post, is the link to the logo present?
It seems the connection between post and logo got lost?


Dan Davies comments:

Alas I've not returned the current image set for a custom meta field in the post editor, so couldn't tell you.


Just Me comments:

Did you check if there is a mix up with lower- and uppercase characters? In your image url.