Support

Account

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

Solved

Save_post and a choice field with dynamic taxonomies

  • Hi,

    I’m making a rather complex structure where i need to order by City, then by Organisation, and then by type of event. It has to be usable in a front-end form as well. I have figured this all out using the great documentation by ACF and WP, but i still stumbled onto a small issue.

    I am using City as a taxonomy, with the cities as terms, and the organisations as child terms. Then I dynamically populate a choice field to only show the child terms, so they can’t make a mistake front end of selecting only a city.

    This way I get the functionality I wish, but the taxonomy selections don’t get saved to the post. This should be good as, if they are saved, i can use the taxonomy template pages to filter the events by city and by organisation.

    I’ve already discovered this post, but I cant seem to get the code working for my case so it seems…

    This is my code to dynamically populate the choice field:

    function acf_load_color_field_choices( $field ) {
    
      // reset choices
      $field['choices'] = array();
    
      $taxonomy = 'gemeente';
    
      $args = array(
        'taxonomy' => 'gemeente',
        'type' => 'programma',
        'hide_empty' => false,
      );
      //get the child categores
      $categories = get_categories($args);
    
      foreach ($categories as $category) {
    
        // var_dump($category);
        $term_children = get_term_children( $category->cat_ID, $taxonomy );
    
        foreach ( $term_children as $child ) {
          $term = get_term_by( 'id', $child, $taxonomy );
          $term_id = $term->term_id;
          $term_name = $term->name;
    
          $field['choices'][ $term_id ] = $term_name;
    
        }
    
      }
    
        // return the field
        return $field;
    
    }
    
    add_filter('acf/load_field/name=organisatie', 'acf_load_color_field_choices');

    This is the code i’m trying to achieve the selection of taxonomy terms with (just copy-pasted from the other post, but i’ve tried filling in the right field with no success of course):

    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);

    Can anyone tell me what i’m doing wrong? The save_post code seems logically to me, i’ve also did some poking around and some print_r’s, it seems its retrieving ID’s and everything…

    Thank you!
    Mathieu

  • Could someone help me with this? Would be very apreciated!

    M

  • 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);
  • $htmlAfter = '<input type="hidden" name="tax_name" id="tax_name" value="ENTER_MAIN_TAXONOMY_SLUG">';
    $htmlAfter .= '<input type="hidden" name="tax_slug" id="tax_slug" value="ENTER_INDIVIDUAL_TAXONOMY_SLUG">';

    'html_after_fields' => $htmlAfter

    function my_acf_save_post( $post_id ) {
        
       if(isset($_POST['tax_name']) && isset($_POST['tax_slug'])) {
    	 wp_set_object_terms( $post_id, $_POST['tax_slug'], $_POST['tax_name'], true );
    	}
        
    }
    
    // run after ACF saves the $_POST['acf'] data
    add_action('acf/save_post', 'my_acf_save_post', 20);

    I know it might not be 100% related, but this idea could be applied in MANY ways, considering where ever you include acf_form, you could use whatever variables are available and then include that data in input values and then include those inputs in the html_after_fields or html_before_fields parameter for the acf_form function.

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

The topic ‘Save_post and a choice field with dynamic taxonomies’ is closed to new replies.