Search code examples
wordpresstaxonomy

How to connect attachment and users through taxonomy in wordpress


I want to restrict attachment access to related users. So the admin can after uploading a file select users to relate them to the attachment.

My plan was to do it with taxonomy but when i do it like this:

$attachment_taxonomies[] = array(
                'taxonomy'  => 'attachment_user',
                'post_type' => 'attachment',
                'args'      => $args
            );

the user is not a real user, its simply some kind of text.

Is it possibly to do it with taxonomies or is there a better way?


Solution

  • I solved it with post meta data instead of a taxonomy.

    // add meta box for attachment
    add_action( 'add_meta_boxes', array( $this, 'add_user_meta_box' ) );
    
    // save data from meta box
    add_action( 'edit_attachment', array( $this, 'save_user_meta_box'), 10, 3 );
    
    // Add the custom columns to the media post type:
    add_filter( 'manage_media_columns', array( $this, 'set_custom_user_column' ) );
    
    // Add the data to the custom columns for the media post type:
    add_action( 'manage_media_custom_column' , array( $this, 'custom_attachment_column' ), 10, 2 );
    

    To get dropdown with users you can use wp_dropdown_users