Support

Account

Forum Replies Created

  • Hi,

    Did you manage to find a way to do this?

    Trying to do the same sort of thing myself.

    Thanks

  • 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);
Viewing 2 posts - 1 through 2 (of 2 total)