Title: posts_where
Published: April 25, 2014
Last modified: May 20, 2026

---

# apply_filters_ref_array( ‘posts_where’, string $where, WP_Query $query )

## In this article

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

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

Filters the WHERE clause of the query.

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

 `$where`string

The WHERE 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_where/?output_format=md#more-information)󠁿

 * This filter applies to the posts where clause and allows you to restrict which
   posts will show up in various areas of the site. Combined with `restrict_manage_posts`
   it allows you to only show posts matching specific conditions.
 * Here is an example to match the `restrict_manage_posts` example:

    ```php
    add_filter( 'posts_where' , 'posts_where' );

    function posts_where( $where ) {

    if( is_admin() ) {
    global $wpdb;

    if ( isset( $_GET['author_restrict_posts'] ) && !empty( $_GET['author_restrict_posts'] ) && intval( $_GET['author_restrict_posts'] ) != 0 ) {
    $author = intval( $_GET['author_restrict_posts'] );

    $where .= " AND ID IN (SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id=$author )";
    }
    }
    return $where;
    }
    ```

Depending on setup, if we had a custom post type of type ‘book’ with a taxonomy (
category style) of type ‘author’, this filter would allow us to only show books 
written by a specific author.

  Certain functions which retrieve posts do not run filters, so the posts_where 
  filter functions you attach will not modify the query. To overcome this, set suppress_filters
  to false in the argument array passed to the function. The following code sample
  illustrates this.
      ```php
      //some function that modifies the query
      function useless_condition ( $where ) { return $where . ' AND 1=1 '; }
  
      //attach your function to the posts_where filter
      add_filter( 'posts_where' , 'useless_condition' );
  
      //get posts AND make sure filters are NOT suppressed
      $posts = get_posts( array( 'suppress_filters' => FALSE ) );
      ```
  
  
  ## 󠀁[Source](https://developer.wordpress.org/reference/hooks/posts_where/?output_format=md#source)󠁿
  
      ```php
      $where = apply_filters_ref_array( 'posts_where', array( $where, &$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/7.0/src/wp-includes/class-wp-query.php#L2780)
  [View on GitHub](https://github.com/WordPress/wordpress-develop/blob/7.0/src/wp-includes/class-wp-query.php#L2780-L2780)
  
  ## 󠀁[Related](https://developer.wordpress.org/reference/hooks/posts_where/?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_where/?output_format=md#changelog)󠁿
  
  | Version | Description | 
  | [1.5.0](https://developer.wordpress.org/reference/since/1.5.0/)
  | Introduced. |
  
  ## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/posts_where/?output_format=md#user-contributed-notes)󠁿
  
   1.  [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/posts_where/?output_format=md#comment-content-3491)
   2.   [Aurovrata Venet](https://profiles.wordpress.org/aurovrata/)  [  7 years ago  ](https://developer.wordpress.org/reference/hooks/posts_where/#comment-3491)
   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_where%2F%23comment-3491)
      Vote results for this note: 2[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_where%2F%23comment-3491)
   4. Please note that the post_type query variable is not set for standard types (
      page, post and attachment, see [WP_QUery](https://developer.wordpress.org/reference/classes/wp_query/)
      class file line 2396 on [trac](https://core.trac.wordpress.org/browser/tags/5.3/src/wp-includes/class-wp-query.php#L2396)),
      only for custom post types, so this is how you can test for each type,
   5.     ```php
              add_filter( 'posts_where' , 'posts_where', 10, 2);
              function posts_where( $args, $wp_query_obj ) {
                $type = $wp_query_obj->query_vars['post_type'];
                switch(true){
                  case 'any'==$type: //query any type (see codex for more details).
                    break;
                  case !empty($type) && is_array($type):
                    //multiple post types define
                    break;
                  case !empty($type):
                    //post type is a custom post.
                    break;
                  case $wp_query_obj->is_attachment():
                    //post type is empty but is a media.
                    $type='attachment';
                    break;
                  case $wp_query_obj->is_page():
                    //post type is empty but is a page.
                    $type='page';
                    break;
                  default:
                    //post type is empty and not an attachment nor a page, so is a post.
                    $type='post';
                    break;
                }
                return $where;
              }
          ```
      
   6.  * Actually the above is incomplete, post_types are not being set for taxonomy
         archive queries. This is a bug which has been filed on [core trac](https://core.trac.wordpress.org/ticket/50070),
       * [Aurovrata Venet](https://profiles.wordpress.org/aurovrata/) [6 years ago](https://developer.wordpress.org/reference/hooks/posts_where/#comment-3908)
   7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_where%2F%3Freplytocom%3D3491%23feedback-editor-3491)
  You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_where%2F)
  before being able to contribute a note or feedback.