I found! this will only change the upload dir when upload in the “rsg_download” CPT
add_filter( 'wp_handle_upload_prefilter', 'rsg_pre_upload' );
function rsg_pre_upload( $file ) {
add_filter( 'upload_dir', 'rsg_custom_upload_dir' );
return $file;
}
function rsg_custom_upload_dir( $param ) {
$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
if( "rsg_download" == get_post_type( $id ) || "rsg_download" == get_post_type( $parent ) ) {
$mydir="/rsg-uploads";
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
}
return $param;
}
i got some trouble when i used any Framework for creating Metaboxes i tried Vafpress Redux CBM2 but the Problem was on my side
here is how i got working for any custom upload fields
function rsg_custom_upload_dir( $param ) {
$current_page = $_SERVER['HTTP_REFERER'];
$id = $_REQUEST['post_id'];
$parent = get_post( $id )->post_parent;
if ( "rsg_download" == get_post_type( $id ) || "rsg_download" == get_post_type( $parent ) ) {
$mydir="/rsg_uploads";
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
} elseif ( strpos( $current_page, 'rsg_download' ) ) {
$mydir="/rsg_uploads";
$param['path'] = $param['basedir'] . $mydir;
$param['url'] = $param['baseurl'] . $mydir;
}
return $param;
}