-2

This is my code and I am getting duplicate values.

$results = array();
    $results['targetOptions'] = array();
    $selectOptions = array();
    $selectOptions = get_posts(
                        array(
                            'post_type' => 'collection',
                            'post_status' => 'publish',
                            'posts_per_page' => -1,
                            'post_parent' => 0,                 
                        )
                    );

                    $results['targetOptions'][] = array("text" => __('Select a Collection', 'theme'), "value" => '');

                    foreach ($selectOptions as $selectOption) :
                        if($sourceVal == '999999') {
                            $terms = get_the_terms( $selectOption->ID, 'window_style' ); 
                        }else {
                            $terms = get_the_terms( $selectOption->ID, 'door_style' );
                        }
                        foreach($terms as $term) {

                          $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $selectOption->ID . '--other_product_style--' . $sourceVal);
                        }

                    endforeach;

                    $results['targetClass'] = '.dynamic-collection';
Naveenbos
  • 2,532
  • 3
  • 34
  • 60

1 Answers1

0
$selectOptions = get_posts(
                    array(
                        'post_type' => 'collection',
                        'post_status' => 'publish',
                        'posts_per_page' => -1,
                        'post_parent' => 0,                 
                    )
                );

                $results['targetOptions'][] = array("text" => __('Select a Collection', 'theme'), "value" => '');

                $term_value_windows = array();
                $term_value_doors = array();
                foreach ($selectOptions as $selectOption) :
                    if($sourceVal == '999999') {
                        $terms = get_the_terms( $selectOption->ID, 'window_style' ); 
                        foreach($terms as $term) {

                          if(in_array($term->name, $term_value_windows)){
                            continue;
                          } else{
                            $term_value_windows[]=$term->name;                          
                            $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $term->term_id . '--other_product_style--' . $sourceVal);
                          }

                        }
                    }else {
                        $terms = get_the_terms( $selectOption->ID, 'door_style' );
                        foreach($terms as $term) {

                          if(in_array($term->name, $term_value_doors)){
                            continue;
                          } else{
                            $term_value_doors[]=$term->name;

                            $results['targetOptions'][] = array("text" => $term->name, "value" => 'other-product--' . $term->term_id . '--other_product_style--' . $sourceVal);
                          }

                        }
                    }       


                endforeach;


                $results['targetClass'] = '.dynamic-collection';

This is the answe for that question.

Naveenbos
  • 2,532
  • 3
  • 34
  • 60