Title: Eliminating duplicate posts from SQL query
Last modified: August 19, 2016

---

# Eliminating duplicate posts from SQL query

 *  Resolved [Elio Rivero](https://wordpress.org/support/users/ilovecolors/)
 * (@ilovecolors)
 * [17 years, 6 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/)
 * Hi everyone, I’m using this code
 *     ```
       function miniposts_join($posts) {
           global $wpdb;
           if (is_home()) {
       		$posts .= "
       		LEFT JOIN $wpdb->term_relationships AS relacion ON ($wpdb->posts.ID = relacion.object_id  )
       		LEFT JOIN $wpdb->term_taxonomy AS taxonomia ON(relacion.term_taxonomy_id = taxonomia.term_taxonomy_id)
       		";
       	}
           return $posts;
       }
       function miniposts_where($posts) {
           if (is_home()) {
               $posts .= " AND taxonomia.term_id <> 12
       		AND taxonomia.taxonomy = 'category'
       		";
           }
           return $posts;
       }
       ```
   
 * but it throws duplicate posts. The duplicate posts arise whenever they are in
   two or more categories. Since I’m not fluent in sql I need to ask you, how can
   I tell the db that I only want those posts once?

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [jbjweb](https://wordpress.org/support/users/jbjweb/)
 * (@jbjweb)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/#post-940048)
 * Maybe this should help? [http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts](http://www.wprecipes.com/how-to-use-two-or-more-loops-without-duplicate-posts)
 *  Thread Starter [Elio Rivero](https://wordpress.org/support/users/ilovecolors/)
 * (@ilovecolors)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/#post-940069)
 * Thanks Jean-Baptiste, it’s interesting and indeed it’s effective, but I’m using
   this in a plugin, filtering the content from there with the calls
 *     ```
       add_filter('posts_join', 'miniposts_join');
       add_filter('posts_where',  'miniposts_where');
       ```
   
 * I need to exclude with SQL the rows that gets duplicated when they belong to 
   more than one category. Maybe this can not be done just by adding the other tables
   and I should replace the default query from within my plugin? if so, should I
   use the_posts filter hook?
 *  Thread Starter [Elio Rivero](https://wordpress.org/support/users/ilovecolors/)
 * (@ilovecolors)
 * [17 years, 5 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/#post-940093)
 * Woohoo! I’ve solved it! I’ve spent some long minutes in the sql query window 
   from phpmyadmin doing some queries to see the results that each query throw, 
   and (yes, kill me please) just by adding `GROUP BY 'ID'` or, in WordPress code`
   GROUP BY $wpdb->posts.ID` I got the result I wanted, that is, no more duplicate
   posts.
 *  [gofree](https://wordpress.org/support/users/gofree/)
 * (@gofree)
 * [16 years, 10 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/#post-940338)
 * can you share your plugin? I have duplicate posts.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Eliminating duplicate posts from SQL query’ is closed to new replies.

## Tags

 * [custom-query](https://wordpress.org/support/topic-tag/custom-query/)
 * [left join](https://wordpress.org/support/topic-tag/left-join/)
 * [sql](https://wordpress.org/support/topic-tag/sql/)

 * 4 replies
 * 3 participants
 * Last reply from: [gofree](https://wordpress.org/support/users/gofree/)
 * Last activity: [16 years, 10 months ago](https://wordpress.org/support/topic/eliminating-duplicate-posts-from-sql-query/#post-940338)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
