apply_filters_ref_array( 'posts_where', string $where, WP_Query $this )

Filters the WHERE clause of the query.


Parameters Parameters

$where

(string) The WHERE clause of the query.

$this

(WP_Query) The WP_Query instance (passed by reference).


Top ↑

Source Source

File: wp-includes/class-wp-query.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Aurovrata Venet

    Please note that the post_type query variable is not set for standard types (page, post and attachment, see WP_QUery class file line 2396 on trac), only for custom post types, so this is how you can test for each type,

        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;
        }
    

You must log in before being able to contribute a note or feedback.