Support

Account

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

Solved

Taxonomy field – only show children of a parent category

  • Hi,

    I am using the taxonomy field and taxonomy ‘category’ to create a form. I have it all working but I wondered if there was a way to only show categories from a parent category.

    For example the front end currently shows the following:

    Date:

    • 1800
    • 1900

    Type:

    • Type 1
    • Type 2

    I only want to show the categories from the Type parent category, i.e. only show Type 1 and Type 2.

    Essentially in the Edit field when you select Taxonomy type and then category, it would then give you a new select box with a Parent category option.

    Any help much appreciated,

  • Hi,
    Also very interested by having help on that topic!! 🙂

  • 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: http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/

    I then used the save_post function to save the data back to the post. http://www.advancedcustomfields.com/resources/acfsave_post/

    /**
     * dynamically set options for date select field
     * @param array $field
     * @return type
     * @see http://www.advancedcustomfields.com/resources/dynamically-populate-a-select-fields-choices/
     */
    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 http://www.advancedcustomfields.com/resources/acf_save_post/
     */
    function my_acf_save_post($post_id) {
    
        // bail early if no ACF data
        if (empty($_POST['acf'])) {
            return;
        }
    
        //array of category id's
        $categoryIds = array();
    
        //value of 'date' field is the cat_id
        $dateCatId = get_field("date");//http://www.advancedcustomfields.com/resources/get_field/
        array_push($categoryIds, $dateCatId);
    
        //set value of category fields onto post
        //http://codex.wordpress.org/Function_Reference/wp_set_post_categories
        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);
  • I have created a ad on plugin for showing the child categories.

    In this you can first select taxonomy and then it will show all the categories just below the taxonomy field. here you can select the parent category to whose children you want to show in the form field.
    Please tell me if you want support. Thanks

  • Hi shailkdangi,

    That is exactly what i need. Could you please help me. Thanks so much

  • you can add me on skype ‘adroittechnosoft’ and let me see how can i help you.
    Thanks

  • Hi, have you posted this plugin publicly? It would be a very useful asset for the community!

    Thanks.

  • @shailkdangi, you should put it on github at least, even if you don’t want to add it to the WP repo.

  • yes i have plugin, you can connect me on skype, ‘adroittechnosoft’

  • Anybody can take plugin from http://adroittechnosoft.com/product/acf-child-category-field/,
    and use coupon code 75-COUPON with 75% discount. Thanks

  • All looks great, but have questions about being able to select & show Parent & also child, like example: http://prntscr.com/pg95ae

  • Hi @shailkdangi i How can this be done so we can select Parent and then also next selection to be the Child?
    So when make a listing we can show the Parent and also the Child

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

The topic ‘Taxonomy field – only show children of a parent category’ is closed to new replies.