Support

Account

Home Forums Backend Issues (wp-admin) Add category to file and filter media library

Solved

Add category to file and filter media library

  • Hi,

    Are there hooks to control uploaded files and media library when add a file ?

    I would like to add a specific category (custom taxo manage by WP Media Category Management) to each file uploaded in file field and only display this category on media library when uploading a file.

    Thanks.

  • Not sure I understand what you are looking for. Can you give move detail or an example. Is WP Media Category Management another plugin? Are you trying to use the fields that this other plugin add to the media library?

  • To make attachment management easier, I put it into categories*.

    I have a ACF form with a file field. When file button is hit, media library modal box appears (I don’t know how to simple have a file uploader in backend like in frontend with ‘uploader’ => ‘basic’). I would like to only display files of this CPT and, obviously, a specific category must be added to this file on upload.

    Is it more understandable ?

    (*) attachment support categories with register_taxonomy_for_object_type( ‘category’, ‘attachment’ ) ; I prefer to use WP Media Category Management for that, because it add select field to filter attachment by category.

  • The media library is only shown for those that can have access to it. If the user is a subscriber or some other type of user that is not allowed to edit media then the simple upload field will be used. Try testing this by logging in as the user type you plan on allowing this form for.

    Also when creating a field you can specify to only allow files that are attached to the post.

    As far as adding a specific category when the file is uploaded. I’m not entirely sure you can do that. You might try creating either an acf/pre_save_post http://www.advancedcustomfields.com/resources/acf-pre_save_post/ or an acf/save_post http://www.advancedcustomfields.com/resources/acfsave_post/ action. I’m not sure which one would be better for this. In this action you’d need to get the attachment ID value from the ACF field and then add the category to the attachment post using wp_set_object_terms() https://codex.wordpress.org/Function_Reference/wp_set_post_terms

  • I didn’t know about file modal according to user level : it’s perfect as is.

    I already try “attached to the post” but I see no attachment at all. “attached to post” means “attached to CPT” or “attached to post id” ? If it’s post id, it’s normal there is nothing in media library when creating a new post.

    Ok for pre_save and save with wp_set_object_terms, I will look at this.

    Thanks for your help.

  • Attached to post, or images/files uploaded to post means files uploaded and attached to the specific post id. If no images have been uploaded then none will be show, as you say, when creating a new post.

Viewing 6 posts - 1 through 6 (of 6 total)

The topic ‘Add category to file and filter media library’ is closed to new replies.