Support

Account

Home Forums Backend Issues (wp-admin) Taxonomy not saving on Attachment

Solved

Taxonomy not saving on Attachment

  • Hello,
    I have added some taxonomies to the attachment post type.

    Example:

    $labels = array(
        'name' => _x( 'Styles', 'taxonomy general name' ),
        'singular_name' => _x( 'Styles', 'taxonomy singular name' ),
        'search_items' =>  __( 'Search Interiors ' ),
        'popular_items' => __( 'Popular Interiors ' ),
        'all_items' => __( 'All Styles' ),
        'parent_item' => null,
        'parent_item_colon' => null,
        'edit_item' => __( 'Edit Styles' ), 
        'update_item' => __( 'Update Styles Type' ),
        'add_new_item' => __( 'Add New Styles Type' ),
        'new_item_name' => __( 'New Styles Type Name' ),
        'separate_items_with_commas' => __( 'Separate Styles Types with commas' ),
        'add_or_remove_items' => __( 'Add or remove Styles Types' ),
        'choose_from_most_used' => __( 'Choose from the most used Styles' ),
        'menu_name' => __( 'Styles Types' ),
      ); 
    
    	// create a new taxonomy
    	register_taxonomy(
    		'style_types',
    		array("attachment"),
    		
    		
    		array(
    		"hierarchical" => true, 
    			'labels' =>$labels,
    			'sort' => true,
    			'args' => array( 'orderby' => 'term_order' ),
    			'rewrite' => array( 'slug' => 'style' ),
    			'show_admin_column' => 'true'
    		)
    	);

    Pretty standard. They show up fine when editing an image. Check boxes etc

    The issue is WordPress taxonomy management is pretty bad when it comes to uploaded a new image. I was thinking of using the ACF taxonomy field to edit the images once uploaded and added to the Gallery. Your implementation of multi-select is elegant and very user friendly. Theirs is fill in a text field with the slug….

    The issue is the ACF taxonomy field fights with the built in one and does not save. Guessing WP taxonomy save has a higher priority.

    Example 1:
    Go to a post that has a ACF Gallery > Click add to gallery
    Upload the image and use the ACF taxonomy field to add in the taxonomy you want
    Click update

    Taxonomy is not saved.

    Example 2:

    Go to Media Library
    Click on an image
    If you select the taxonomy with the built in check boxes it saves.
    Using ACF does not save.

    Example 3:
    Go to post with gallery
    Click on an existing image. Only the ACF taxonomy shows up, select the taxonomy and update. Works great.

    For small amounts of images this is not a problem. But the final site will have 20-30 per post and want to eliminate double clicking every image and also minimize user error.

    Is there any way to use the taxonomy field if the taxonomy is already on the post?

    Setup of the ACF 5.3.0 Pro fields:
    Posts: Gallery Field. Standard setup

    Images: Taxonomy Field called Styles
    Taxonomy Styles is selected
    Appearance: Multiselect

    Allow Null: No
    Create Terms: no
    Save Terms: Yes
    Load Terms: Yes
    Return Value: Term Object
    Conditional Logic: No

    For location: Attachment is equal to All

  • Hi @nhankla

    Have you tried removing the default taxonomy meta box?
    I’m not sure if it’s possible to do with the attachment as those are as you say a bit different but there’s a function called remove_meta_box() that you can try: https://codex.wordpress.org/Function_Reference/remove_meta_box

  • Hi,
    Thanks Jonathan!!!

    This is the final code that solves the issue:

    // Remove taxonomy from the attachment pages so the acf taxonomy can work
    
    function remove_custom_taxonomy()
    {
    	remove_meta_box('YOURTAXONOMYNAMEdiv', 'attachment', 'side' );
    	
          
           
    }
    add_action( 'admin_menu', 'remove_custom_taxonomy' );
    
    // Add this in to remove it from the popup editor
    function attachment_remove_taxonomy( $fields ) {
    		
    		unset($fields['YOURTAXONOMYNAME']); 
    	  return $fields;
    	}
    add_filter( 'attachment_fields_to_edit', 'attachment_remove_taxonomy' );
  • Great 🙂

    I figured there’d be another filter one had to hook into for the attachments.

    Please mark as resolved and feel free to open up another topic should you need any assistance with ACF.

  • I realise I am necroposting, but I found this answer useful and wanted to post a small improvement.

    The first function remove_custom_taxonomy can be eliminated if meta_box_cb is set to false when making the initial register_taxonomy call.

    Or, if you don’t have control of the registration, you can filter the args later:

    
    function removeTaxonomyInEditor($args, $taxonomy)
    {
         if ($taxonomy === 'INSERTYOURTAXONOMYNAME') {
            $args['meta_box_cb'] = false;
        }
        return $args;
    }
    add_filter('register_taxonomy_args', removeTaxonomyInEditor, 10, 2);
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Taxonomy not saving on Attachment’ is closed to new replies.