Support

Account

Home Forums Feature Requests Conditional Logic using Taxonomy field Reply To: Conditional Logic using Taxonomy field

  • Kind of annoying that this still has not been implemented after almost 3.5 years, but for what it’s worth: +1

    Meanwhile I modified Beee’s solution to accept more than one value as I needed it to work with checkboxes.

    I’m sure someone more experienced can improve on this, but here it is for now:

    
    function change_post_taxonomy( $post_id ) {
        // bail if no ACF data
        if ( empty($_POST['acf']) ) {
            return;
        }
        
        // Limit to certain post types if needed
        if(get_post_type($post_id) == 'whatever') {
    	    
    	    $term_ids = array();
    		
    	    // get term id from $post_id
    	    $stored_role = wp_get_post_terms($post_id, 'category');
    	    
    	    // get submitted value from acf form by field key
    	    $posted_roles = $_POST['acf']['field_582c317422b74'];
    	    
    	    // get term_id for the submitted value(s)	    
    	    foreach ($posted_roles as $posted_role) { 
    			
    			$term = get_term_by( 'name', $posted_role, 'category' );
    			$term_id = $term->term_id;
    			$term_ids[] = $term_id;
    		
    		}
    	    
    	    // if stored value(s) is/are not equal to posted value(s), then update terms
    	    if ( $stored_role != $posted_role ) {
    	        wp_set_object_terms( $post_id, $term_ids, 'category' );
    	    }
        }
    }
    add_action('acf/save_post', 'change_post_taxonomy', 20);