How to upload media to specific year/month folder

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