Support

Account

Home Forums Backend Issues (wp-admin) Save_post and a choice field with dynamic taxonomies Reply To: Save_post and a choice field with dynamic taxonomies

  • Glad to say i’ve solved it myself.

    In stead of pulling post_categories, i had to pull the terms. Since its in a custom post / taxoomy, I had to pull the object_terms in stead of the post terms.

    So I’ve got my terms via $term = get_term_by('id', $dateCatId, 'gemeente'); , gemeente being my taxonomy.

    Next problem was that my wp_get_object_terms read the ID as a string and created a new category with the id as slug. Thats why i pull the ID back out on the term like this:
    wp_set_object_terms($post_id, $term->term_id, 'gemeente');

    Hope i’ve helpen someone with this, and also, if anyone has a better alternative, glad to hear it!

    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('organisatie');//http://www.advancedcustomfields.com/resources/get_field/
        array_push($categoryIds, $dateCatId);
    
        $term = get_term_by('id', $dateCatId, 'gemeente');
    
        wp_set_object_terms($post_id, $term->term_id, 'gemeente');
    }
    
    // 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);