WordPress allows users to organize their uploaded media into month-and-year-based folders. The images that are uploaded from the admin interface (e.g. via Media Library or inside posts and pages) will be automatically uploaded into the folder of the uploading month accordingly. For example, if you upload an image in May 2023, the image will be uploaded to folder /wp-content/uploads/2023/05/.
Automatically uploading images into the current year and month folder is quite helpful in some cases. However, there are also case where we need to upload images into specific month year folder, such as when we have to replace some broken images in the past month or year folder or when we want to put image for scheduled post in the future month or year folder. I found the solution from a stackexchange QnA.
I have tested these steps to upload images to specific year month folder in WordPress version 6.2.2.
Steps to upload images to specific year month folder in WordPress
- Edit your theme's functions.php that is located in \wp-content\themes\your-theme-name folder.
- Add this code to specify the year and month folder:
add_filter( 'upload_dir', function () {
return _wp_upload_dir( '2023/06' );
}, 100, 0 );
Change the '2023/06' in the code above to year/month that you want. - After adding the code to your theme's functions.php, you can upload images to the year month folder you have specified. However, the images' meta data will still say that the uploaded time is the real uploaded time. To change the images' metadata, you need to change some information on the database table:
- Go to
wp_posts
table - Search the image file's name / title in the column
post_title
. - Change the value in
post_date
andpost_date_gmt
columns to your preferable dates.
- Go to
Now you can upload images to specific year month folder with correct metadata.