Support

Account

Home Forums Backend Issues (wp-admin) "Create terms" form: pre-select parent field and/or change the new term object

Solved

"Create terms" form: pre-select parent field and/or change the new term object

  • Hi everyones,

    I deal with an issue that i can’t fix. and i’m pretty sure the solution is close.
    So i need your help !!!

    Here is the context:
    I use acf taxonomy field to associate a custom taxonomy terms (a brand name) to a post (a product).
    – The acf taxonomy field is call “brand” and as the “create terms” option checks. the taxonomy select in the acf admin panel is a custom taxonomy call “detail”
    – This custom taxonomy call “detail” is hierarchical and contains many terms (parent and child). One of this terms is name “ours brands” (term_id 89) and his childs are all the brands that the store sell.

    I use the “acf/fields/taxonomy/wp_list_categories” filter to display in the acf taxonomy field call “brand” only the childs of taxonomy terms “ours brands” (see code below)

    function my_acf_fields_list_query( $args, $field ) {
       	if($field['_name'] == 'marque'){
            $args['include'] = get_term_children( 89, 'detail' );
            $args['orderby'] = 'name';
            $args['order'] = 'ASC';
        }
        return $args;
    }
    add_filter('acf/fields/taxonomy/wp_list_categories', 'my_acf_fields_list_query', 10, 2);

    Everything works fine. The field display only the brand childs as i want.

    My issue is this:
    When a backend user add a product of a brand that is not already exists in db (ie in “ours brand” term childs). He have to create it.
    To create this new brand term, after clicking the “+” button to open the create terms panel, and tape the name of thenew brand, the user have to choose the parent of the new terms in a huge (huge, huge) list of parent and child.
    It will be a source of westing time and errors.

    So here comes my question:
    Is there a way (filter or else), which allow to
    – pre-select the parent field in the “create terms” admin panel based on the acf field that we edit
    and/or
    – change/customize the values (name, slug and parent) of the new term, after the create form sends (and before the new tem is save on db)?

    All ideas are welcome.

    Thanks for reading,
    have a nice day!
    Nauar

  • Since you are using the acf/fields/taxonomy/wp_list_categories hook you can limit the terms available in the “parent” field by using the acf/fields/taxonomy/query filter.

    For anyone that is reading this that is using a select field rather than a checkbox or radio field like the OP and using acf/fields/taxonomy/query to limit selections you can do this by using different filters for each. A simple example

    
    funcition my_taxonomy_filter($args, $field, $post_id) {
      // this is the filter for limiting selection is the taxonomy field
    }
    add_filter('acf/fields/taxonomy/query/field_key=field_XXXXXXX', 'my_taxonomy_filter', 10, 3);
    
    function my_secondary_taxonomy_filter($args, $field, $post_id) {
      // this is the secondary filter used when getting values to show in parents
    }
    // this action is fired when then add term popup is loaded
    // what we want to do here is switch the filter to use when adding a term
    // done with a low priority to switch it before ACF does the query
    add_action('wp_ajax_acf/fields/taxonomy/add_term', 0);
    
    function switch_taxonomy_filters() {
      // switch which filter to use when adding terms
      remove_filter('acf/fields/taxonomy/query/field_key=field_XXXXXXX', 'my_taxonomy_filter', 10);
      add_filter('acf/fields/taxonomy/query/field_key=field_XXXXXXX', 'my_secondary_taxonomy_filter', 10, 3);
    }
    

    You should also not that if you are using the acf/fields/taxonomy/query for your field and you don’t want the parent field filtered that you should still remove the main filter because as you can see, it operates on both the field and the parent and your parents will only show what is available in the main field.

  • Thanks a lot John, for your reply !!!

    I wasn’t aware that the acf/fields/taxonomy/query filter works with ajax based request.
    It’s perfect to solve my issue.

    Have a good day !

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.