• Resolved Elio Rivero

    (@ilovecolors)


    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)
  • Thread Starter Elio Rivero

    (@ilovecolors)

    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

    (@ilovecolors)

    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.

    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.