Home Forums General Issues populate and save dynamically


populate and save dynamically

  • Hi to all,
    just a little suggestion for my configuration.
    What I want to do is create some ACF taxonomy fields in form of checkboxes that when selected will updates terms of my custom post.
    I accomplished this using the default ACF taxonomy field but I need to go a little deeper.

    So.. let’s suppose I have a CPT Appartments with a taxonomy Islands and inside this I have a hierargical structure like

    Corf├╣–> middle town, upper town, lower town.
    Creta–> middle town, upper town, lower town.
    Cefalonia–> middle town, upper town, lower town.

    If I select the default ACF taxonomy field, it will show all the hierargical structure in one field, this is very difficoult to use when I have 20 or 30 towns per islands. So I was thinking to dynamically populate some choice fields with the subcategories(town1 town2 etc) and then apply some conditional rules to show the correct subcategory choice field only if the correct parent category is selected.

    I used this code following this guide

    add_filter('acf/load_field/name=populated-towns', 'populate_choice');
    function populate_choice($field) {
    	static $list = null;
    	// reset choices
      	$field['choices'] = array();
            // return cached list
    	if($list !== null) { return $list; }
            	// initialize choice list
    		$list = array();
    		// find the categories we want to add
    		$terms = get_terms( array(
    		    'taxonomy' => appartment_category,
    		    'hide_empty' => false,
    		    'child_of' => 172 //parent ID
    		) );
    		if(is_array($terms) && (count($terms) > 0)) {
    			foreach($terms as $term) {
    				$list[$term->term_id] = $term->name;
    	// set choice list & return updated field
    	$field['choices'] = $list;
    	return $field;

    at this point the choice field with slug populated-towns is correctly filled with town1, town2 town3 but now is not very clear how to save terms to the selected post, cause obviously in this way only the field will be updated. I followed this
    but I cannot make this code to save

    /*save custom post field*/
    function my_acf_save_post($post_id) {
        // bail early if no ACF data
        if (empty($_POST['acf'])) {
        //array of category id's
        $termIDs = array();
        $termID = get_field('populated-towns'); 
        array_push($termIDs, $termID);
        wp_set_post_terms($post_id, $termIDs);
    add_action('acf/save_post', 'my_acf_save_post', 20);
  • I did it with this code!

    /*save custom post field*/
    function my_acf_save_post( $post_id ) {
    		// An array of IDs of categories we want this post to have.
    		$cat_ids = get_field('localita-popolata');
    		//If this was coming from the database or another source, we would need to make sure
    		//these were integers:
    		$cat_ids = array_map( 'intval', $cat_ids );
    		$cat_ids = array_unique( $cat_ids );
    		wp_set_object_terms( $post_id, $cat_ids, 'struttura_category' );
    add_action('acf/save_post', 'my_acf_save_post', 20);
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘populate and save dynamically’ is closed to new replies.