I'm currently using the set_post_thumbnail() function to insert a programmatically uploaded image to a post. The problem is that the function works perfectly when the user is logged in only.

If the user is not logged in, in the back-end, wordpress says that the file is attached to the post but does not display it as a featured image. I don't have any conditional statement in that function.

Does anybody have had the same issue? Is that a normal behavior?

Edit: Noticed that the issue showed up since I've upgraded Wordpress from 3.5.1 to 3.5.2 but I really don't know if it's related

Edit: Here is a sample of my code

// This part is for testing purpose, the file is well placed with the right permission
$dir = '/home/web/tmpdir';
$data = $_POST['imageCanvas'];
$preview = base64_decode( str_replace( 'data:image/png;base64,', '', $data ) );
$file = '000011.png'; 
$filename = $dir . '/' . $file;
file_put_contents( $filename, $preview );

// Here I copy the test file directly into the wordpress uploads dir
// After this line I find the file in the uploads dir, so, let's say that for now it works as intended
$wp_filetype = wp_check_filetype( basename( $file ), null );
copy( $filename, '/home/web/wordpress/wp-content/uploads/' . basename( $file ) );
$attachment = array(
    "post_mime_type" => $wp_filetype['type'],
    "post_title" => preg_replace( '/\.[^.]+$/', '', basename( $file ) ),
    "post_content" => '',
    "post_status" => 'inherit'

// This part worked well until I recently upgraded WP to 3.5.2...
$attach_id = wp_insert_attachment( $attachment, $file, $lastpostID );
require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );
set_post_thumbnail( $lastpostID, $attach_id );

// This part returns the right ids in Apache logs...
error_log( 'attachid: ' . $attach_id . ' - postid: ' . $lastpostID );

I can find the file attached to his post into the wordpress media library:

Wordpress media library

But if I open the post, I don't see any featured image.

Post featured image

  • 1
    It's unclear as to what you are trying to do. Show us your code? Are you trying to show the featured image on the front end to your readers? – Adam Jun 27 '13 at 4:24
  • Just did, it's probably more clear now. And yes, the featured image is supposed to be shown on the post (on the back end and on the front end too). It was the case before I upgraded WP to 3.5.2. Now it works only if the user who's calling the function is logged in. If not, the file is correctly uploaded, I can find it in the media library but not set as a featured image (post thumbnail). – egdavid Jun 27 '13 at 8:07

It was an issue with W3 Total cache. Since the Database cache was activated for non logged in users only, it was not working as intended anymore.

Now the weird part... The Database cache was activated for months and it always worked well, until I upgraded WP to 3.5.2 (and probably W3TC in the same time).

  • Well glad it's solved. Please mark your own question as correct for general housekeeping. – Adam Jun 27 '13 at 9:33
  • Seems like I have to wait 24 hours before I can tag my answer as correct. – egdavid Jun 27 '13 at 13:17
  • How did you fix this? I have a similar problem with set_post_thumbnail not working when W3 Total Cache is enabled – Max Carroll Apr 6 '20 at 18:02
  • @MaxCarroll I don't really remember, it was 7 years ago and I haven't used W3TC since then. I guess I disabled the Database Cache or even the plugin and used a different one for caching purpose. – egdavid Apr 7 '20 at 5:58
  • 1
    Thanks for responding, I have scoured the internet and not found much, I managed to find a work around by adding a Constant to the Reject Constants section in the database cahce settings. I picked a constant that was only used by my plugin so as not to affect other functionality of the plugin, although this is a temporary fix until I can find a better solution – Max Carroll Apr 7 '20 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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