Home Forums General Issues Taxonomy field – only show children of a parent category Reply To: Taxonomy field – only show children of a parent category

  • I found a way round this by using a ‘Choice’ field and dynamically populating it.
    Essentially I populate the choice field with the categories I want, I can choose if it is a radio, check or select etc, and then on update/publish of the post I get the selections and save them back to the post.

    Using this function you can dynamically populate the choice field:

    I then used the save_post function to save the data back to the post.

     * dynamically set options for date select field
     * @param array $field
     * @return type
     * @see
    function my_acf_date_field($field) {
        //get child categories of category date
        $args = array(
            'type' => 'post',
            'child_of' => 8, //date category parent
            'parent' => '',
            'orderby' => 'name',
            'order' => 'ASC',
            'hide_empty' => 0,
            'hierarchical' => 1,
            'exclude' => '',
            'include' => '',
            'number' => '',
            'taxonomy' => 'category',
            'pad_counts' => false
        //get the child categores 
        $categories = get_categories($args);
        //array of choices to add to field
        $choices = array();
         * add each category to $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;
        //add choices to field
        $field['choices'] = $categories;
        return $field;
    // acf/load_field/name={$field_name} - filter for a specific field based on it's name
    add_filter('acf/load_field/name=date', 'my_acf_date_field');
     * Save the data to the post
     * @param type $post_id
     * @return type
     * @see
    function my_acf_save_post($post_id) {
        // bail early if no ACF data
        if (empty($_POST['acf'])) {
        //array of category id's
        $categoryIds = array();
        //value of 'date' field is the cat_id
        $dateCatId = get_field("date");//
        array_push($categoryIds, $dateCatId);
        //set value of category fields onto post
        wp_set_post_categories($post_id, $categoryIds);
    // run before ACF saves the $_POST['acf'] data
    //add_action('acf/save_post', 'my_acf_save_post', 1);
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);