This is part two to this question: Post Specific Uploader

I am using an upload filter to put files in a location based on post_id. Now when the file uploads, the URL path in the meta is wrong and the image preview is broken (missing the post_id).

the URL shows as: http://foo.com/blog/wp-content/uploads/icon_jpeg.png but should (in this case) be: http://foo.com/blog/wp-content/uploads/571/icon_jpeg.png

This part is truly magic to me. Should the path be correct if those were the value when the file was uploaded? Is there a query param I need to change?

Thanks for helping a WP noob!


1 Answer 1


untested, but it should work theoretically:

add_filter('get_attached_file', function($path, $file, $attachment_id){

  // get the post object of the current attachment
  $att = get_post($attachment_id);

  // prepend attachment post parent ID to the file name
  return substr_replace($path, "{$att->post_parent}/{$file}", -strlen($file));

Another filter, attempts to "fix" the path returned by WP's upload handler:

add_filter('wp_handle_upload', function($results){
  global $post;

    return $results;


  $uploads = wp_upload_dir();

  $file = str_replace($uploads['basedir'], "{$uploads['basedir']}/{$post->ID}", $file);
  $url = str_replace($uploads['baseurl'], "{$uploads['baseurl']}/{$post->ID}", $url);

  return compact('file', 'url', 'type');

This will not work if the media uploader doesn't expose the current post inside the global $post variable.

  • Ill give it a shot! So it looks as if i am grabbing the post var in transit? Here is the odd thing, if i close the result window, and open it again, the preview path is right, and the image IS there! Apr 5, 2012 at 3:31
  • before I give this a shot can you walk thru this or comment? does get_attached_file only affect the file path immediately after upload? .. sigh 15 rep to upvote... Apr 5, 2012 at 13:47
  • That function should be called every time you request an image from an attachment. It will get the file name of the image (which is a meta value of the attachment), then it appends it to the wp upload path to generate the full image path. What function are you using to display these images? Apr 5, 2012 at 14:55
  • it only needs to be fixed immediately after the upload. Right after I upload, the uploader shows the image and the path, but they are both wrong. BUT, if I close the media uploader window, then reopen it, the path and the thumbnail do appear. I guess I could strstr() for the presence of the post_id and if not there, then alter. I think my question is, why is it happening only immediately after the download. Thanks for your help! Apr 5, 2012 at 15:19
  • This is because the uploader will update the current page based on the paths returned by the upload handler. I added another filter for that, it probably won't work though... Apr 5, 2012 at 15:44

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.