I am trying to find a way to upload few selective images to selective folders in WordPress but not able to find a way so far.
I do not want to change default work press setting to upload images as that is needed in all cases.
Working on a travel site where i need to show images of destinations on destination introduction page.Proposed structure.
- Each Destination will belong to a state.
- Each destinations can have multiple posts.
So for e.g user want to see details about Taj Mahal, so there can be multiple posts about Taj Mahal but will going to have a common landing page for Taj Mahal with a list of posts/articles about the place.
On landing page i want to show few images about that destination, and i want to store these images in following structure
wp-contentuploadsstate-namedestination
PS:For each destination, there will be few introduction images (2-3 max), and i want to upload them to specific folder.
Is it possible or do i need to create these folder structure manually
In your answer you said that desired path is something like
where
state-name
is a taxonomy term anddestination
is the slug of one destination CPT.Last thing is not clear from your question, but it seems so, let me know if I’m wrong).
So, I suggest you this workflow:
Add a destination post -> assign a state term -> after that, upload the image using the media uploader from the destination post just created.
Doing so, we can retrieve the destination ID for current attachment (passed to media uploader as
$_GET['post_id']
), than retrieve the assigned state term, and finally set the folder using the'upload_dir'
filter.Something easy:
In this way, when you upload images from a destination post, if the destination post as a state assigned (as taxonomy term) the image will uploaded in the folder
if the destination post has no state assigned, the image will be saved in the folder
If more states are assigned to destination, the first state is taken.
Note that this code assume the cpt name for destinations is,
'destinations'
and the taxonomy name for states is'states'
.