How to upload media to specific year/month folder

The question:

This answer describes a hack in which it is possible to upload new media to an “old” folder like 2012/12. That hack no longer seems to work in recent WP versions. Meanwhile, all the plugins I can find for media/folder management seem to want to create an entirely new folder hierarchy, and bypass the native WordPress year/month system completely.

I just want to upload some lost media from original files outside WordPress into an “old” year/month folder associated with the publication date of an old article long since published, but missing its media somehow. I’d much prefer that to linking from within the old article to some new location, because that is just going to raise questions from some people.

So far my only option seems to be to downgrade WordPress and try to find a version that supports that “hack” in the link above. Is there a better way?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Okay, so here’s a hack to get what you want. There are a few caveats though, so please be sure to take account of this:

  • this is untested with WPMS. I haven’t a clue how this will affect it.
  • it uses a so-called “private” WordPress function, which means that the functionality of this function may change with any WP release and you can’t rely on it to always work as you expect. So test it before you roll it out on any scale.
  • I recommend using this and then removing it when you don’t need it any longer, or if you leave it in-place, ALL your media will go to the same directory.
  • code assumes PHP 5.3 and above.
  • code assumes WordPress 4.5 and above.
  • code style is not adhering to WordPress code styles for hooks as it’s meant to be put in-place very temporarily and removed.

It works simply by filtering and forcefully replacing the uploads directory with the date you want.

add_filter( 'upload_dir', function () {
    return _wp_upload_dir( '2021/01' );
}, 100, 0 );

How to use it?

  1. Put the code in your theme’s functions.php
  2. Simply replace 2021/01 with the YYYY/MM you want to store your images.
  3. Test uploading an image to see that it works as you’d expect by placing it in the correctly dated folder.
  4. Upload your images
  5. Remove this code from your functions.php

Method 2

You’re talking about ‘lost’ media. Do you mean that the posts that contain that media are still present and just the files are missing? i.e. the media is in the media library also, but it’s just a 404 on the file?

You could then just upload the original image to the proper directory (check devtools for images with 404 and their file paths) with an (s)ftp client and then regenerate all the images in WordPress with e.g. the Regenerate plugin

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment