• Im trying to use this to create separate upload directory for custom post type “gallery”. Its working fine excepts it creates two folders – one with post title and other with image title. Images are in the post title folder as needed. How can I prevent creating image title folder?
    Thanks

    function custom_upload_directory( $args ) {
    
        $id = $_REQUEST['post_id'];
        $parent = get_post( $id )->post_parent;
        $post_title = get_the_title($id);
    
        // Check the post-type of the current post
        if( "gallery" == get_post_type( $id ) || "gallery" == get_post_type( $parent ) ) {
            $args['path'] = WP_CONTENT_DIR . '/downloads/' . $post_title;
            $args['url']  = WP_CONTENT_URL . '/downloads/' . $post_title;
            $args['basedir'] = WP_CONTENT_DIR . '/downloads/';
            $args['baseurl'] = WP_CONTENT_URL . '/downloads/';
        }
        return $args;
    }
    add_filter( 'upload_dir', 'custom_upload_directory' );

The topic ‘Custom upload directory with post title / need help for code’ is closed to new replies.