Title: posts_groupby
Published: April 25, 2014
Last modified: February 24, 2026

---

# apply_filters_ref_array( ‘posts_groupby’, string $groupby, WP_Query $query )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#wp--skip-link--target)

Filters the GROUP BY clause of the query.

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#parameters)󠁿

 `$groupby`string

The GROUP BY clause of the query.

`$query`[WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)

The [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/) instance(
passed by reference).

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#more-information)󠁿

 * If you come with MySQL knowledge, the `GROUP BY` clause is pretty useless without
   the ability to modify the `SELECT` statement.
 * There is no `SELECT` filter since the query is supposed to return only the post
   data. The `GROUP BY` clause is set _only when_ there are [Custom Field Parameters](https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters)
   for querying by post meta or [Taxonomy Parameters](https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters)
   for querying by taxonomy.
 * The default `posts_groupby` is set to `{$wpdb->posts}.ID`, which means that even
   if there are multiple results because of multiple meta and taxonomy, they are
   grouped together by the post id.

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#source)󠁿

    ```php
    $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-query.php#L2945)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-query.php#L2945-L2945)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Query::get_posts()](https://developer.wordpress.org/reference/classes/wp_query/get_posts/)`wp-includes/class-wp-query.php` |

Retrieves an array of posts based on query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/posts_groupby/?output_format=md#comment-content-4750)
 2.    [Steven Lin](https://profiles.wordpress.org/stevenlinx/)  [  5 years ago  ](https://developer.wordpress.org/reference/hooks/posts_groupby/#comment-4750)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_groupby%2F%23comment-4750)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_groupby%2F%23comment-4750)
 4.  Example migrated from Codex:
 5.      ```php
         add_filter( 'posts_groupby', 'my_posts_groupby' );
     
         function my_posts_groupby($groupby) {
             global $wpdb;
             $groupby = "{$wpdb->posts}.ID";
             return $groupby;
         }
         ```
     
 6.  The code above will just set the GROUP BY clause whether or not, a [taxonomy query](https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters)
     or a [meta query](https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters)
     is present.
 7.  For example, say we have a custom table (for ratings) and we wish to filter the
     posts using data from this table (only show posts that have a 5 star rating).
 8.  We can use the [posts_join](https://developer.wordpress.org/reference/hooks/posts_join/)
     filter to join the tables. If there are multiple entries in the ratings table,
     the join can return multiple results for the same post.
 9.  We can make sure that we only have one row per post (that has all the entries 
     for the ratings) by setting the `GROUP BY` clause. Remember that in the default
     query, `GROUP BY` clause is only set when there is a [meta query](https://developer.wordpress.org/reference/classes/wp_query/#custom-field-post-meta-parameters)
     or [taxonomy query](https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters)
     involved.
 10.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_groupby%2F%3Freplytocom%3D4750%23feedback-editor-4750)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_groupby%2F)
before being able to contribute a note or feedback.