Support

Account

Home Forums Backend Issues (wp-admin) Update select options

Unread

Update select options

  • I’m trying to get only sub categories from a parent category in a custom field but I can’t get the result.

    My field is a single select field, with “allow null” option enabled.
    My code:

    function filter_by_stato_field($field) {
        $args = array(
            'type' => 'film',
            'child_of' => 55, //category parent
            'parent' => '',
            'orderby' => 'name',
            'order' => 'ASC',
            'hide_empty' => 0,
            'hierarchical' => 1,
            'exclude' => '',
            'include' => '',
            'number' => '',
            'taxonomy' => 'category',
            'pad_counts' => false
        );
        $categories = get_categories($args);
    
        $choices = array();
    
        foreach ($categories as $category) {
            // var_dump($category);
            $id = $category->cat_ID;//set value as category id
            $name = $category->cat_name;//label as category name
            $choices[$id] = $name;
        }
        $field['choices'] = $choices;
    	   return $field;
    }
    
    add_filter('acf/load_field/name=film-stato-pubblicazione', 'filter_by_stato_field');

    I get the results from the query and correctly populate $field array.
    But when I check in my post editing screen, I still find all categories and not only sub categories from ID=55 parent.

    Can you help me?
    Thanks in advance

Viewing 1 post (of 1 total)

The topic ‘Update select options’ is closed to new replies.