Home > Mobile >  Can you display an ACF field in a taxonomy relationship field in the admin panel?
Can you display an ACF field in a taxonomy relationship field in the admin panel?

Time:09-22

I have a taxonomy for city names. They have an ACF field for the state. But when you are editing a post type with this taxonomy associated with it, you only see the city name in the taxonomy select list. This can be problematic for cities like Portland, where there are at least 2 in the USA. Same for Springfield, etc.

Does anyone know if I can apply some hook to display the ACF field with the title in the admin panel when you are editing a post type?

The image below should illustrate the needs. Basically, I just want it to say Portland, ME instead of just Portland (and likewise for other cities). But I need the name separate in the fields so I can't just name the cities with their states respectively.

Anyone done this before? enter image description here

I tried googling and using the extended plugin for ACF but pretty much found results for adding columns in the taxonomy edit screen, which I don't need. It's just in the taxonomy module on the side of a post when you're editing that needs some kind of extra field, or ability to concatenate the title with an ACF field.

UPDATE

Thanks to Orbital's help, I was able to get this to do what I needed. The is_admin part wasn't working because the list is retrieved by ajax so I had to get a little sneaky. This may not be the most secure way to do it, but the function is harmless so I am not concerned about that. Final code below:

if( current_user_can( 'manage_options' ) && strpos($_SERVER['REQUEST_URI'], "wp-json" ) == 1 ) {
    add_filter('get_term', 'change_term_name_at_admin', 10, 2);

    function change_term_name_at_admin($term, $taxonomy) {
        if ($taxonomy == 'markets') {
            $key_field_acf = "markets_" . $term->term_id; 

            $state_name = get_field( 'state', $key_field_acf );

            if ($state_name != "") {
                $new_name = $term->name . ', ' . $state_name;
                $term->name = $new_name;
            }
        }
        return $term;
    }
}

enter image description here

CodePudding user response:

Try this code

We change display format only for Wordpress admin for your_taxonomy_name to "Term name - State name"

add_filter('get_term', 'change_term_name_at_admin', 10, 2);

function change_term_name_at_admin($term, $taxonomy) {
    //only for admin, not front
    if( is_admin() ) {


   if ($taxonomy == 'your_taxonomy_name') {

            $key_field_acf = "your_taxonomy_name_" . $term->term_id; 

            $state_name = get_field( 'state_name ', $key_field_acf );

            if ($new_name !="") {
             $new name = $term->name . ' -' . $state_name;
                $term->name = $new_name;
            }

   }
}
    return $term;
}

Change in this line

if ($taxonomy == 'your_taxonomy_name') {

and in this line

$key_field_acf = "your_taxonomy_name_" . $term->term_id;

your_taxonomy_name to your cities taxonomy name

  • Related