Support

Account

Home Forums Add-ons Repeater Field Update_field for Taxonomies in a Repeater Field only saving the last instance

Unread

Update_field for Taxonomies in a Repeater Field only saving the last instance

  • Hello, I’ve been having this issue with my front-end form saving taxonomies onto the page. I’ve gotten it to save the taxonomy into its row, and for it to create a new taxonomy if what’s inputted doesn’t already exist, but why it isn’t saving the taxonomy onto the page is eluding me.

    $ingredients = array();
      $ingredientsTax = array();
      $ingredientsData = $_POST['ingredient'];
    
      foreach ($ingredientsData as $key => $list) {
        // CREATE LIST
        $newlist = array();
        $newCollection = array();
        foreach ($list as $key => $item) {
          
          if (is_array($item)) {
            $newIngredient = array();
              if (!empty($item['qty'])) {
                $newIngredient['field_590a6016de776'] = $item['qty'];
              };
              if (!empty($item['units']) && !empty($item['qty'])) { // must include quantity
                $newIngredient['field_590a603ade777'] = $item['units'];
              };
              if (term_exists((int)$item['ingredient'], 'ingredients')) {
                $newIngredient['field_590a6008de775'] = (int)$item['ingredient'];
              } elseif ($item['ingredient'] != '') {
                $newTerm = wp_insert_term($item['ingredient'], 'ingredients');
                $newIngredient['field_590a6008de775'] = $newTerm['term_id'];
              };
    
              if (!empty($item['directions'])) {
                $newIngredient['field_590a6192de778'] = $item['directions'];
              };
              
              if ($item['optional'] != '') {
                $newIngredient['field_590d45e28701e'] = $item['optional'];
              };
              if ($item['featured'] != '') {
                $newIngredient['field_592a69568401a'] = $item['featured'];
              };
            array_push($newCollection, $newIngredient);
            
            $newCollection = array_filter(array_map('array_filter', $newCollection)); // Strip out empty entries
            
            $newlist['field_590a5feede774'] = $newCollection;
          } else {
            $newlist['field_590a5f7cde772'] = $list['title'];
            $newlist['field_590a603ade777'] = $list['directions'];
          }
          
        };
        
        array_push($ingredients, $newlist);
    
      };
      
      
      print_r($ingredients);
    update_field('field_590a5f0cde771', $ingredients, $new_post_id);

    That may output an array that looks more or less like:

    
    						
    
    Array
    (
        [0] => Array
            (
                [field_590a5f7cde772] => 
                [field_590a603ade777] => 
                [field_590a5feede774] => Array
                    (
                        [0] => Array
                            (
                                [field_590a6016de776] => 4
                                [field_590a603ade777] => unit
                                [field_590a6008de775] => 217
                            )
    
                        [1] => Array
                            (
                                [field_590a6016de776] => 0.01
                                [field_590a603ade777] => unit
                                [field_590a6008de775] => 225
                            )
    
                        [2] => Array
                            (
                                [field_590a6016de776] => 6
                                [field_590a603ade777] => unit
                                [field_590a6008de775] => 53
                            )
    
                    )
    
            )
    
    )
    

    The problem is it only ever seems to save the last taxonomy onto the page (in this case, ID 53, and does not include 217, and 225). The fields do show up in the backend and if I re-save the page behind the scenes, the taxonomies are registered properly.

    Is there something I’m missing?

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.