Based on your comments, it sounds like you just want a custom directory for your post type called client_gallery
which is fairly straight-forward actually. The below just uses the upload_dir
hook to achieve this:
/**
* Modify Upload Directory Based on Post Type
*
* @param Array $dir
*
* @return Array $dir
*/
function wpse_247197( $dir ) {
$request_arr = array( 'upload-attachment', 'editpost', 'editedtag' );
$request = array_change_key_case( $_REQUEST, CASE_LOWER ); // WordPress uses post_id and post_ID
$type = null;
// Are we where we want to be?
if( ! ( isset( $_REQUEST['action'] ) && in_array( $_REQUEST['action'], $request_arr ) ) ) {
return $dir;
}
if( isset( $request['post_id'] ) ) { // Get Post ID
$id = $request['post_id'];
if( isset( $request['post_type'] ) ) { // Get the post type if it was passed
$type = $request['post_type'];
} else { // If not passed, get post type by ID
$type = get_post_type( $id );
}
} else { // If we can't get a post ID return default directory
return $dir;
}
if( isset( $type, $id ) && ! empty( $type ) && ! empty( $id ) ) {
// Here we can test our type and change the directory name etc. if we really wanted to
if( 'product' != $type ) {
return $dir;
}
$uploads = apply_filters( "{$type}_upload_directory", "{$type}/{$id}" ); // Set our uploads URL for this type
$dir['url'] = path_join( $dir['baseurl'], $uploads ); // Apply the URL to the directory array
$dir['path'] = path_join( $dir['basedir'], $uploads ); // Apply the Path to the directory array
error_log( print_r( $dir, 1 ) );
}
return $dir;
}
add_filter( 'upload_dir', 'wpse_247197' );
I’ve heavily commented the above code so should you have any questions regarding it you may ask in the comments. The idea is that whenever a user uploads to the post type itself it will get uploaded directly to the folder. This is not the case with assigning a pre-uploaded attachment media – moving the files once they’ve already been uploaded and the URLs have been set would be extremely troublesome.