Support

Account

Home Forums Add-ons Flexible Content Field Filter taxonomy field in flexible content by parent category

Solved

Filter taxonomy field in flexible content by parent category

  • Within Flexible content, I have a taxonomy field type, but this returns ALL categories, which is fine if you only have a few categories of the same type.

    But when you have lots of categories that are grouped, showing all the categories is unnecessary and confusing.

    So, in my case, I have several categories under “membership levels”, but also other categories for news. In the flexible content field, I want the user to select from a list that only includes the membership levels – having all the “news” categories listed makes no sense.

    Is there a way to natively filter this field so it shows a list of taxonomies that includes ONLY the children of a parent category?

    Thank you

  • You can limit the terms shown using one of these two filters

    if you are using a select field – acf/fields/taxonomy/query

    If you are using a radio or checkbox field – acf/fields/taxonomy/wp_list_categories

  • John, thanks for the response. I read through the link you provided and worked it out! In my case, I was able to add the following code, and because I linked it to the key of the field, it only affected that one set of radio buttons. Perfect! Here’s my code:

    add_filter(‘acf/fields/taxonomy/wp_list_categories/key=field_69ee9943be86f’, ‘my_acf_fields_taxonomy_query’, 10, 2);
    function my_acf_fields_taxonomy_query( $args, $field ) {

    // Order by most used.
    $args[‘child_of’] = ’14’;
    $args[‘orderby’] = ‘title’;
    $args[‘order’] = ‘ASC’;

    return $args;
    }

  • Sorry for getting back to you late. Glad that you worked it out.

  • John – is there any way to INCLUDE the parent category in the list?

  • You would need to build an array of term IDs to use in the “include” argument

    
    $children = get_term_children($term_id, 'TAXONOMY');
    $include = array_merge(array($term_id), $children);
    $args['include'] = $include;
    
Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.