Support

Account

Home Forums Backend Issues (wp-admin) Change Taxonomy Terms to Slugs in Admin Post Editor

Solving

Change Taxonomy Terms to Slugs in Admin Post Editor

  • I’ve got a taxonomy (‘business-type’) that I’m using the ACF Taxonomy selector to let users choose their type.
    These terms are also an NAICS code, so I’m using the ‘slugs’ as the code (so it would be like: ‘Air Transportation’ is the term, and ‘481’ is the slug).

    Here’s the problem.
    The site’s admins would like to be able to add terms to the posts by picking the NAICS number.

    So essentially, I need to be able to show the Taxonomy Slugs in a normal hierarchical (checkbox) select list in the post editor.

    Is this possible at all??

    To make it more difficult, we want to have the Taxonomy field for Terms for the normal user to use, and a ‘admin-only’ Taxonomy field for Slugs.

    Am I completely hopeless here?

    Any thoughts?

  • Hi @aaronrobb

    Problems are meant to be solved ey 🙂

    I dived into the taxonomy field code and unfortunately it looks like you wont be able to do this when you have it set as checkboxes.

    It might be possible tho if you’re open to change the visuals to multi-select instead? There are some filters one can hook into but none of them are at work on the checkbox layout since that actually uses the wp_list_categories() function from WP core (with a custom walker function).

  • Hey Jonathan

    Ok, we could see how the multi-select works.
    What filter would be used to modify this to include the slug?

    And to modify my first post, we only need the one set of terms to show.

    Here’s the filter from the documentation. Is this what we use?

    <?php
    
    function my_taxonomy_query( $args, $field, $post_id ) {
        
        // modify args
        $args['orderby'] = 'count';
        $args['order'] = 'ASC';
        
        
        // return
        return $args;
        
    }
    
    add_filter('acf/fields/taxonomy/query', 'my_taxonomy_query');
    
    ?>
  • Ok, so there is a hack way of doing what I want.

    In taxonomy.php i modified line 1020 from this:

    		// append
    		$output .= '<li data-id="' . $term->term_id . '"><label><input type="' . $this->field['field_type'] . '" name="' . $this->field['name'] . '" value="' . $term->term_id . '" ' . ($selected ? 'checked="checked"' : '') . ' /> <span>' . $term->name . '</span></label>';
    				
    	}

    to this:

    // append
    		$output .= '<li data-id="' . $term->term_id . '"><label><input type="' . $this->field['field_type'] . '" name="' . $this->field['name'] . '" value="' . $term->term_id . '" ' . ($selected ? 'checked="checked"' : '') . ' /> <span>'. $term->slug .'-' . $term->name . '</span></label>';
    				
    	}

    adding in the ‘$term->slug’ in the label span.

    So now a term looks like:

    1132-Forest Nurseries and Gathering of Forest Products

    With 1132 being the slug.

    This isn’t a good solution, since it could get overwritten, so just need to find out how to make that work….
    Especially because I have a second Taxonomy field (different taxonomy) that I don’t want the slug to show up on….

  • Hi @aaronrobb

    Changing in core files is never a good thing since it’ll be overwritten each time you update (and ACF updates are pushed out quite regularly).

    The taxonomy query filter is really only if you want to change what to query, not how the results should look.

    If you’re open to switching to multiselect dropdown I think there’s a few filters you need to use.

    First is the http://www.advancedcustomfields.com/resources/acfload_field/
    Where you’ll want to modify the [‘choices’] array.

    And since it uses AJAX to fetch more taxonomies you need to modify the output there as well using this filter:
    acf/fields/taxonomy/result/name=name_of_field

    which takes these parameters $title, $term, $field, $post_id and should return $title.

    In there you can also check for the users role by doing

    
    $user = wp_get_current_user();
    if ( in_array( 'author', (array) $user->roles ) ) {
        //The user has the "author" role
    }
    
Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Change Taxonomy Terms to Slugs in Admin Post Editor’ is closed to new replies.