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

As of now my uploaded files are organized by year and month. But I would like to organize them by year, month and day. I mean like this 2012/10/30/image goes here

PS: There is a plugin available to do this. But I don't want to use a plugin for this simple task. Thanks

share|improve this question
@kaiser Sorry.. Corrected it.. I'm not going to use the code in my theme functions but going to use it in my mu-plugins. – Giri Oct 30 '12 at 12:21
I already corrected that for you (it's the other way 'round ;) ) - rolled back. – kaiser Oct 30 '12 at 12:26
up vote 2 down vote accepted

Code based in other Answer of mine and this SO Answer.

It uses the post/page/cpt publish date to build the paths.
Note that $the_post->post_date_gmt is also available.

add_filter('wp_handle_upload_prefilter', 'wpse_70946_handle_upload_prefilter');
add_filter('wp_handle_upload', 'wpse_70946_handle_upload');

function wpse_70946_handle_upload_prefilter( $file )
    add_filter('upload_dir', 'wpse_70946_custom_upload_dir');
    return $file;

function wpse_70946_handle_upload( $fileinfo )
    remove_filter('upload_dir', 'wpse_70946_custom_upload_dir');
    return $fileinfo;

function wpse_70946_custom_upload_dir($path)
     * Determines if uploading from inside a post/page/cpt - if not, default Upload folder is used
    $use_default_dir = ( isset($_REQUEST['post_id'] ) && $_REQUEST['post_id'] == 0 ) ? true : false; 
    if( !empty( $path['error'] ) || $use_default_dir )
        return $path; // Error: not uploading from a post/page/cpt 

    $the_post = get_post( $_REQUEST['post_id'] );
    $y = date( 'Y', strtotime( $the_post->post_date ) );
    $m = date( 'm', strtotime( $the_post->post_date ) );
    $d = date( 'd', strtotime( $the_post->post_date ) );

    $customdir = '/' . $y . '/' . $m . '/' . $d;

    $path['path']    = str_replace($path['subdir'], '', $path['path']); //remove default subdir (year/month)
    $path['url']     = str_replace($path['subdir'], '', $path['url']);      
    $path['subdir']  = $customdir;
    $path['path']   .= $customdir; 
    $path['url']    .= $customdir;  

    return $path;
share|improve this answer
Hi thanks for the code. But its not working in my site. I'm using the magic field plugin for custom fields. This plugin handles my image uploads. It uses wp_upload_dir() function to store the images. Right now my image uploads are organized by year and month. I'm using your code in my mu-plugins folder. But when I upload a file its still not using day folder. Its not at all creating day folder. Am i missing something? Do i have to uncheck "Organize my uploads into month- and year-based folders" in media settings? – Giri Oct 31 '12 at 21:51
If the plugin "handles the image uploads", maybe we have a conflict of interests here... Can you post the relevant code it uses for that? – brasofilo Oct 31 '12 at 21:59
Please Note line number 19,30,31 of this file and line 86,87 of this file. The second one has the main upload function of that plugin. Thanks – Giri Nov 1 '12 at 0:09
At a first glance, I don't see a way to surpass those plugin constants... I doubt, but try to give a late priority to the hooks, i.e., add_filter('wp_handle_upload', 'wpse_70946_handle_upload', 9999 );. :::: This code works for me in TwentyEleven without any active plugins and with Media Settings "Organize my uploads..." checked/unchecked. :::: If the code works for you without Magic Fields being active, then I guess you'll need to open a new Question or a support request... :/ – brasofilo Nov 1 '12 at 0:27
Ok Thanks.. I'll give it a try. And one more weird thing. As i said earlier I put your code in my mu-plugins folder. My server time now is 00:44 Nov 1. I see some new folders 1970/01/01 and my existing image links are broken. Any idea? – Giri Nov 1 '12 at 0:51

As far as I can tell, the filter 'upload_dir' is only called once in wp-includes\functions.php

I find this solution to be much simpler. It will place all uploads into the year/month/day directory for the date it is uploaded.

function upload_dir_filter($uploads){
    $day = date('d');
    $uploads['path'] .= '/' . $day;
    $uploads['url']  .= '/' . $day;
    return $uploads;
add_filter('upload_dir', 'upload_dir_filter');
share|improve this answer

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.