2

Wordpress, Archive Cateory page. From default WP_Query->get_posts() we are getting something like this:

SELECT wp_posts.ID
FROM wp_posts
INNER JOIN wp_term_relationships
ON (wp_posts.ID = wp_term_relationships.object_id)
WHERE 1=1
AND ( wp_term_relationships.term_taxonomy_id IN (5) )
AND wp_posts.post_type = 'post'
AND ((wp_posts.post_status = 'publish'))
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
LIMIT 0, 40

I want to remove this: "GROUP BY wp_posts.ID" from this SQL query, is it possible? I've read about pre_get_posts and posts_groupby filter, but I see there is only way to change GROUP BY value, but how to remove it absolutely?

1
  • The posts_groupby filter should work. Try returning an empty string when calling that filter. Commented May 26, 2015 at 22:03

1 Answer 1

4

You can use posts_groupby filter:

add_filter( 'posts_groupby', function( $groupby ) {

            return '';

} );

If you want to apply to remove the GROUP BY only to main query and in the frontend, you could do something like this:

add_filter( 'posts_groupby', function( $groupby ) {

    if( ! is_admin() && is_main_query() ) {

            $groupby = '';

    }

    return $groupby;

} );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.