Title: remove duplicates from loop
Last modified: August 30, 2016

---

# remove duplicates from loop

 *  [tayssir.ch](https://wordpress.org/support/users/tayssirch/)
 * (@tayssirch)
 * [10 years, 10 months ago](https://wordpress.org/support/topic/remove-duplicates-from-loop/)
 * in my wordpress site, i have categories like this :
    Parent Category 1 – Sub 
   Category 1 – Title – Title – Sub Category 2 – Title – Title Parent Category 2–
   Sub Category 1 – Title – Title – Sub Category 2 – Title – Title Category 3 – 
   Title – Title
 * I want to display post, for each term as term > display post
    but i don’t want
   to display Parent Category , i want a loop to be like this: – Sub Category 1 –
   Title – Title – Sub Category 2 – Title – Title
 * sometimes posts are associated with 2 category, how to remove duplicates ?
 *     ```
       <?php
       $do_not_duplicate = array();
       //get all categories then display all posts in each term
       $taxonomy = 'category';
       $param_type = 'category__in';
       $term_args=array(
   
       );
       $terms = get_terms($taxonomy,$term_args);
       if ($terms) {
         foreach( $terms as $term ) {
           $args=array(
             "$param_type" => array($term->term_id),
             'connected_type' => 'posts_to_invitations',
             'connected_items' => get_queried_object(),
             'nopaging' => true,
             'post_type' => 'post',
             'post_status' => 'publish,draft',
             'posts_per_page' => -1,
             'post__not_in'     => $do_not_duplicate,
             'caller_get_posts'=> 1
             );
           $my_query = null;
           $my_query = new WP_Query($args);
           if( $my_query->have_posts() ) {
       	$do_not_duplicate[] = get_the_ID();	?>
       <?php
             while ($my_query->have_posts()) : $my_query->the_post(); ?>
       		<tr>
       				<td><?php include (TEMPLATEPATH . '/contactfullname.php'); ?></td>
       				<td><?php echo get_post_meta($post->ID, 'ecpt_position', true); ?></td>
       				<td><?php echo p2p_get_meta( get_post()->p2p_id, 'table', true ); ?></td>
       				<td><?php echo ' ' .$term->name;?></td>
       				<td><?php echo p2p_get_meta( get_post()->p2p_id, 'confirmation', true ); ?></td>
       		</tr>
              <?php
             endwhile;
             ?>
       <?php
           }
         }
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   

The topic ‘remove duplicates from loop’ is closed to new replies.

## Tags

 * [duplicate](https://wordpress.org/support/topic-tag/duplicate/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [tayssir.ch](https://wordpress.org/support/users/tayssirch/)
 * Last activity: [10 years, 10 months ago](https://wordpress.org/support/topic/remove-duplicates-from-loop/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
