Home › Forums › Backend Issues (wp-admin) › Add category to file and filter media library
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.
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.
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
CPT registration is coming to ACF! We demoed the new feature during the most recent session of ACF Chat Fridays. Check out the summary for the details. https://t.co/k2KQ3WWBAz— Advanced Custom Fields (@wp_acf) March 7, 2023
© 2023 Advanced Custom Fields.