How to Upload Images to Specific Month Folder in WordPress

Updated on 9 June 2023
Wordpress logo

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

  1. Edit your theme's functions.php that is located in \wp-content\themes\your-theme-name folder.
  2. 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.
  3. 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:
    1. Go to wp_posts table
    2. Search the image file's name / title in the column post_title.
    3. Change the value in post_date and post_date_gmt columns to your preferable dates.

Now you can upload images to specific year month folder with correct metadata.

First published by  on Last Modified on 9 June 2023.

Add A Comment

Your email address will not be published. Required fields are marked *