Support

Account

Home Forums ACF PRO Taxonomy fields not saving

Unread

Taxonomy fields not saving

  • Hi, sorry for the long explanation, I’m trying to summarize the issue as much as possible.

    In a custom post type I have a couple of taxonomy fields (one admitting a single value, and one admitting multiple values), plus a repeater field that displays another taxonomy field on each new row.

    Initially, when saving, the first two taxonomy fields where saving correctly. I had the ‘save terms’ option turned on, and in the terms list I could see the post being connected to its matching terms.

    However, the taxonomy in the repeater was not saving. Apparently, I’m paraphrasing, this is an expected outcome (?) that needs a filter to get it to work properly. I another thread I found a function to enable the saving.

    Now I’m able to save the taxonomy values in the repeater field. However, my other two taxonomy fields stopped saving.

    After much fiddling I extended the function to save the repeater field taxonomy, to also save my other two individual taxonomy fields. Also I had to disable the ‘save terms’ and ‘load terms’ in all the taxonomies. It works but I still feel it is strange I had to solve my problem this way.

    Is there a reason for this? A better approach to solve my problem?

    This is the filter I ended up using to save all the taxonomies, including the one in the repeat field:

    
    // Ensures that taxonomy fields saves properly.
    // Currently they are not saving well.
    // Needs to be applied to each post with an ACF taxonomy field
    // see more @ https://bit.ly/3bnxQsg
    
    function update_cpt_post_terms($post_id) {
      if (get_post_type($post_id) != 'proyectos') {
        return;
      }
    
      $terms_amenities = NULL; // this will clear terms if none found
      $terms_status = NULL; // this will clear terms if none found
      $terms_certificaciones= NULL;
    
      if (have_rows('amenities', $post_id)) { // loops through the repeater for amenities
        $terms_amenities = array();
        while (have_rows('amenities', $post_id)) {
          // add this term to the array
          $terms_amenities[] = get_sub_field('amenity_name', false); // false for no formatting
        }
      }
      set_object_terms($post_id, $terms_amenities, 'amenities', false);
    
      $status = get_field( 'estatus', $post_id ); // only 1 estatus value is allowed. So we get the value directly
      if ( $status ){
        $terms_status[] = $status;
      }
      set_object_terms($post_id, $terms_status, 'estatuses', false);
    
      $certificaciones = get_field('certificaciones', $post_id); // there can be multiple certificaciones, so we have to loop
      if ($certificaciones){
        foreach($certificaciones as $certificacion):
            $terms_certificaciones = $certificacion;
        endforeach;
      }
      set_object_terms($post_id, $terms_certificaciones, 'certificaciones', false);
    
    }
    
    add_action('acf/save_post', 'update_cpt_post_terms'); // Fixes issue with taxonomies not saving in some CPTs
    
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.