You should be able to change the upload directory for uploads to be https://example.com/uploads/advert/file.png instead of https://example.com/uploads/2019/05/20/file.png by adding the code below in your theme functions.php file
function wpse_16722_type_upload_dir( $args ) {
// Get the current post_id
$id = ( isset( $_REQUEST['post_id'] ) ? $_REQUEST['post_id'] : '' );
if( is_numeric( $id ) ) {
// Set the new path depends on current post_type
$newdir = '/' . get_post_type( $id );
$args['path'] = str_replace( $args['subdir'], '', $args['path'] ); //remove default subdir
$args['url'] = str_replace( $args['subdir'], '', $args['url'] );
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
} else if( $id === "false" && isset( $_REQUEST['action'] ) && $_REQUEST['action'] == 'adverts_gallery_upload' ) {
$newdir = '/advert';
$args['subdir'] = $newdir;
$args['path'] .= $newdir;
$args['url'] .= $newdir;
}
return $args;
}
add_filter( 'upload_dir', 'wpse_16722_type_upload_dir' );
Few notes the code is based on this thread on StackExchange https://wordpress.stackexchange.com/questions/76895/different-upload-directory-based-on-post-type-in-a-theme, it will change the upload directory for pages, posts and any custom post type if you want this to affect only WPAdverts you need to customize it.
One way to do that would be to replace is_numeric( $id ) with get_post_type( $id ) === "advert"
Thread Starter
bdd
(@bws-online)
Thanks, but that didn’t work. It put the image into the same place as before.
I am not sure, i am testing this code on my dev site and it seems to be working fine all the WPAdverts files go to wp-content/uploads/advert/ folder.
The only thing i can suggest is to remove this part $id === "false" && from the code snippet and try again.
Thread Starter
bdd
(@bws-online)
Still not working. Curious that it would work for you but not for me.
(I do see an advert folder in the uploads folder, it just doesn’t have any files in it.)
Thread Starter
bdd
(@bws-online)
Tried all sorts of tweaks to the code, different examples from other posts, etc. — nothing was working. I was doing all this in my theme functions.php because that’s what you’d said to do. Finally decided just to try it as a plugin — and it worked.
So thanks, looks to be set now.