Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am developing a project management program for my engineering company. Clients and jobs are custom post types.

I have a custom uploader that I have created using wordpress's native uploader to work as a file manager for each and every client or job in the system loading only files that have been uploaded to only the client / job being viewed in the system.

I would need to have a multiple libraries for each one.

For example: One for permitting filess and one for general files that might be uploaded on a job. As permitting files only to be viewable to a team of people who deal with permitting issues.

Another example: I want to allow multiple people be able to view what clients are in the system and upload certain files that be be of interest but only a select few should have access to view pricing information and contracts that contain sensitive information.

From looking at these functions I have ran across and modified slightly, it may not be possible to do what I want without a core 'hack'.


 * Change Upload Directory for Custom Post-Type
 * This will change the upload directory for a custom post-type. Attachments will
 * now be uploaded to an "uploads" directory within the folder of your plugin. Make
 * sure you swap out "post-type" in the if-statement with the appropriate value...
 * -- This has been edited to create new folder for each
 * client / post type / etc under their post id.
 * -- This function works in conjunction with limitMediaLibraryItems_56456
 * to ensure only the current post type's files are being shown in the file manager
 * @see upload_dir directory WP filter
function custom_upload_directory( $args ) {

    $id = $_REQUEST['post_id'];
    $parent = get_post( $id )->post_parent;

    // Check the post-type of the current post
    if( "rpm_client" == get_post_type( $id ) || "rpm_client" == get_post_type( $parent ) ) {
        $args['path'] = plugin_dir_path(__FILE__) . "uploads/clients/" . $id;
        $args['url']  = plugin_dir_url(__FILE__) . "uploads/clients/" . $id;
        $args['basedir'] = plugin_dir_path(__FILE__) . "uploads/clients/" . $id;
        $args['baseurl'] = plugin_dir_url(__FILE__) . "uploads/clients/" . $id;
    return $args;
add_filter( 'upload_dir', 'custom_upload_directory' );

 * added - 11/27/13
 * -- This function works in conjunction with custom_upload_directory to
 * ensure only the current post type's files are being shown in the file manager
 * @see posts_where WP filter
function limitMediaLibraryItems_56456($where, &$wp_query) {
    global $pagenow, $wpdb;
    $id = $_REQUEST['post_id'];

    // Do not modify $where for non-media library requests
    if ($pagenow !== 'admin-ajax.php') {
        return $where;

    // Check post type and direct to correct folder.
    if("rpm_client" == get_post_type( $id )){
        $where .= " AND {$wpdb->posts}.guid LIKE '%{$id}%'";
    }elseif("rfd_job" == get_post_type( $id )){
        $where .= " AND {$wpdb->posts}.guid LIKE '%{$id}%'";
    return $where;
add_filter('posts_where', 'limitMediaLibraryItems_56456', 10, 2 );

Anyone have any input that might aide me on this issue?

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.