Taxonomy Loops | Showing Duplicate Data

I am Looping over two taxonomies to get all the posts attached to the respective taxonomies. However in the second taxanomy I get duplicate data.

Here is the code I am using:

$tax1 = get_terms(array(
        'taxonomy' => 'mmh-oses',
        'hide_empty' => true
    ));

    $tax2 = get_terms(array(
        'taxonomy' => 'mmh-brands',
        'hide_empty' => true
    ));

    $tax1Array = array();

    foreach ($tax1 as $taxs1) {
        $tax2Array = array();
        foreach ($tax2 as $taxs2) {

            $handsets_args = array(
                'showposts' => -1,
                'post_type' => 'xxx',
                'post_status' => 'publish',
                'hide_empty' => true,
                'tax_query' => array(
                    array(
                        'taxonomy' => $taxs2->taxonomy,
                        'field' => 'term_id',
                        'terms' => $taxs2->term_id
                    ),
                    array(
                        'taxonomy' => $taxs1->taxonomy, // taxonomy name
                        'field' => 'term_id', // term_id, slug or name
                        'terms' => $taxs1->term_id
                    )
                )
            );

            $posts       = get_posts($args);
            $postsArray = array();
            foreach ($posts as $post) {
                $postsArray[] = array(
                    'id' => (string) $post->ID,
                    'name' => $post->post_title
                );

            }

            if (has_term($tax2->slug, $tax2->taxonomy, $post->ID)) {
                $tax2Array[] = array(
                    'name' => $tax2->name,
                    'devices' => $postsArray
                );
            }
        }

        $packagesArray[] = array(
            'name' => $taxs1->name,
            'tax2' => $taxs2Array
        );

    }

    $jsonOutput = array(
        'platforms' => $packagesArray
    );

    echo json_encode($jsonOutput, JSON_UNESCAPED_SLASHES);

In the output, in the second tax2 I get same data I get for the first tax2.

Sample output:

{  
   "platforms":[  
      {  
         "name":"abc",
         "tax2":[  
            {  
               "name":"tax2_detail1",
               "devices":[  
                  {  
                     "id":"28664",
                     "name":"XXXXXXXXXXXXXX"
                  }
               ]
            }
         ]
      },
      {  
         "name":"xyz",
         "brand":[  
            {  
               "name":"tax2_detail1",
               "devices":[  

               ]
            },
            {  
               "name":"tax2_detail2",
               "devices":[  
                  {  
                     "id":"28065",
                     "name":"XXXXXX"
                  },

As you can see tax2_detail1 is repeated.

I can see all loops correct, any idea what i’m doing wrong ?

Read more here: Taxonomy Loops | Showing Duplicate Data

Leave a Reply

Your email address will not be published. Required fields are marked *