Make WordPress Core

Ticket #37699: 37699-query.diff

File 37699-query.diff, 26.5 KB (added by wonderboymusic, 8 years ago)
  • src/wp-includes/query.php

     
    13211321        private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
    13221322
    13231323        /**
     1324         * @since 4.7.0
     1325         * @access protected
     1326         * @var wpdb
     1327         */
     1328        protected $db;
     1329
     1330        /**
    13241331         * Resets query flags to false.
    13251332         *
    13261333         * The query flags are what page info WordPress was able to figure out.
     
    21142121         *
    21152122         * @since 3.7.0
    21162123         *
    2117          * @global wpdb $wpdb WordPress database abstraction object.
    2118          *
    21192124         * @param array $q Query variables.
    21202125         * @return string WHERE clause.
    21212126         */
    21222127        protected function parse_search( &$q ) {
    2123                 global $wpdb;
    2124 
    21252128                $search = '';
    21262129
    21272130                // added slashes screw with quote grouping when done early, so done later
     
    21612164                        }
    21622165
    21632166                        if ( $n && $include ) {
    2164                                 $like = '%' . $wpdb->esc_like( $term ) . '%';
    2165                                 $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
     2167                                $like = '%' . $this->db->esc_like( $term ) . '%';
     2168                                $q['search_orderby_title'][] = $this->db->prepare( "{$this->db->posts}.post_title LIKE %s", $like );
    21662169                        }
    21672170
    2168                         $like = $n . $wpdb->esc_like( $term ) . $n;
    2169                         $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title $like_op %s) $andor_op ($wpdb->posts.post_excerpt $like_op %s) $andor_op ($wpdb->posts.post_content $like_op %s))", $like, $like, $like );
     2171                        $like = $n . $this->db->esc_like( $term ) . $n;
     2172                        $search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ($this->db->posts.post_content $like_op %s))", $like, $like, $like );
    21702173                        $searchand = ' AND ';
    21712174                }
    21722175
     
    21732176                if ( ! empty( $search ) ) {
    21742177                        $search = " AND ({$search}) ";
    21752178                        if ( ! is_user_logged_in() )
    2176                                 $search .= " AND ($wpdb->posts.post_password = '') ";
     2179                                $search .= " AND ({$this->db->posts}.post_password = '') ";
    21772180                }
    21782181
    21792182                return $search;
     
    22562259        /**
    22572260         * Generate SQL for the ORDER BY condition based on passed search terms.
    22582261         *
    2259          * @global wpdb $wpdb WordPress database abstraction object.
    2260          *
    22612262         * @param array $q Query variables.
    22622263         * @return string ORDER BY clause.
    22632264         */
    22642265        protected function parse_search_order( &$q ) {
    2265                 global $wpdb;
    2266 
    22672266                if ( $q['search_terms_count'] > 1 ) {
    22682267                        $num_terms = count( $q['search_orderby_title'] );
    22692268
     
    22702269                        // If the search terms contain negative queries, don't bother ordering by sentence matches.
    22712270                        $like = '';
    22722271                        if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
    2273                                 $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
     2272                                $like = '%' . $this->db->esc_like( $q['s'] ) . '%';
    22742273                        }
    22752274
    22762275                        $search_orderby = '';
     
    22772276
    22782277                        // sentence match in 'post_title'
    22792278                        if ( $like ) {
    2280                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
     2279                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_title LIKE %s THEN 1 ", $like );
    22812280                        }
    22822281
    22832282                        // sanity limit, sort as sentence when more than 6 terms
     
    22922291
    22932292                        // Sentence match in 'post_content' and 'post_excerpt'.
    22942293                        if ( $like ) {
    2295                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_excerpt LIKE %s THEN 4 ", $like );
    2296                                 $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 5 ", $like );
     2294                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_excerpt LIKE %s THEN 4 ", $like );
     2295                                $search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_content LIKE %s THEN 5 ", $like );
    22972296                        }
    22982297
    22992298                        if ( $search_orderby ) {
     
    23142313         * @since 4.0.0
    23152314         * @access protected
    23162315         *
    2317          * @global wpdb $wpdb WordPress database abstraction object.
    2318          *
    23192316         * @param string $orderby Alias for the field to order by.
    23202317         * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
    23212318         */
    23222319        protected function parse_orderby( $orderby ) {
    2323                 global $wpdb;
    2324 
    23252320                // Used to filter values.
    23262321                $allowed_keys = array(
    23272322                        'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
     
    23682363                        case 'ID':
    23692364                        case 'menu_order':
    23702365                        case 'comment_count':
    2371                                 $orderby_clause = "$wpdb->posts.{$orderby}";
     2366                                $orderby_clause = "{$this->db->posts}.{$orderby}";
    23722367                                break;
    23732368                        case 'rand':
    23742369                                $orderby_clause = 'RAND()';
     
    23932388                                        $orderby_clause = $orderby;
    23942389                                } else {
    23952390                                        // Default: order by post field.
    2396                                         $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
     2391                                        $orderby_clause = "{$this->db->posts}.post_" . sanitize_key( $orderby );
    23972392                                }
    23982393
    23992394                                break;
     
    24802475         * @since 1.5.0
    24812476         * @access public
    24822477         *
    2483          * @global wpdb $wpdb WordPress database abstraction object.
    2484          *
    24852478         * @return array List of posts.
    24862479         */
    24872480        public function get_posts() {
    2488                 global $wpdb;
    2489 
    24902481                $this->parse_query();
    24912482
    24922483                /**
     
    26222613
    26232614                switch ( $q['fields'] ) {
    26242615                        case 'ids':
    2625                                 $fields = "$wpdb->posts.ID";
     2616                                $fields = "{$this->db->posts}.ID";
    26262617                                break;
    26272618                        case 'id=>parent':
    2628                                 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
     2619                                $fields = "{$this->db->posts}.ID, {$this->db->posts}.post_parent";
    26292620                                break;
    26302621                        default:
    2631                                 $fields = "$wpdb->posts.*";
     2622                                $fields = "{$this->db->posts}.*";
    26322623                }
    26332624
    26342625                if ( '' !== $q['menu_order'] )
    2635                         $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
     2626                        $where .= " AND {$this->db->posts}.menu_order = " . $q['menu_order'];
    26362627
    26372628                // The "m" parameter is meant for months but accepts datetimes of varying specificity
    26382629                if ( $q['m'] ) {
    2639                         $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
     2630                        $where .= " AND YEAR({$this->db->posts}.post_date)=" . substr($q['m'], 0, 4);
    26402631                        if ( strlen($q['m']) > 5 )
    2641                                 $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
     2632                                $where .= " AND MONTH({$this->db->posts}.post_date)=" . substr($q['m'], 4, 2);
    26422633                        if ( strlen($q['m']) > 7 )
    2643                                 $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
     2634                                $where .= " AND DAYOFMONTH({$this->db->posts}.post_date)=" . substr($q['m'], 6, 2);
    26442635                        if ( strlen($q['m']) > 9 )
    2645                                 $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
     2636                                $where .= " AND HOUR({$this->db->posts}.post_date)=" . substr($q['m'], 8, 2);
    26462637                        if ( strlen($q['m']) > 11 )
    2647                                 $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
     2638                                $where .= " AND MINUTE({$this->db->posts}.post_date)=" . substr($q['m'], 10, 2);
    26482639                        if ( strlen($q['m']) > 13 )
    2649                                 $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
     2640                                $where .= " AND SECOND({$this->db->posts}.post_date)=" . substr($q['m'], 12, 2);
    26502641                }
    26512642
    26522643                // Handle the other individual date parameters
     
    27092700                }
    27102701
    27112702                if ( '' !== $q['title'] ) {
    2712                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_title = %s", stripslashes( $q['title'] ) );
     2703                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_title = %s", stripslashes( $q['title'] ) );
    27132704                }
    27142705
    27152706                // Parameters related to 'post_name'.
    27162707                if ( '' != $q['name'] ) {
    27172708                        $q['name'] = sanitize_title_for_query( $q['name'] );
    2718                         $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
     2709                        $where .= " AND {$this->db->posts}.post_name = '" . $q['name'] . "'";
    27192710                } elseif ( '' != $q['pagename'] ) {
    27202711                        if ( isset($this->queried_object_id) ) {
    27212712                                $reqpage = $this->queried_object_id;
     
    27442735                        if  ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
    27452736                                $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
    27462737                                $q['name'] = $q['pagename'];
    2747                                 $where .= " AND ($wpdb->posts.ID = '$reqpage')";
     2738                                $where .= " AND ({$this->db->posts}.ID = '$reqpage')";
    27482739                                $reqpage_obj = get_post( $reqpage );
    27492740                                if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
    27502741                                        $this->is_attachment = true;
     
    27562747                } elseif ( '' != $q['attachment'] ) {
    27572748                        $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
    27582749                        $q['name'] = $q['attachment'];
    2759                         $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
     2750                        $where .= " AND {$this->db->posts}.post_name = '" . $q['attachment'] . "'";
    27602751                } elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
    27612752                        $q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
    27622753                        $post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
    2763                         $where .= " AND $wpdb->posts.post_name IN ($post_name__in)";
     2754                        $where .= " AND {$this->db->posts}.post_name IN ($post_name__in)";
    27642755                }
    27652756
    27662757                // If an attachment is requested by number, let it supersede any post number.
     
    27692760
    27702761                // If a post number is specified, load that post
    27712762                if ( $q['p'] ) {
    2772                         $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
     2763                        $where .= " AND {$this->db->posts}.ID = " . $q['p'];
    27732764                } elseif ( $q['post__in'] ) {
    27742765                        $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
    2775                         $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
     2766                        $where .= " AND {$this->db->posts}.ID IN ($post__in)";
    27762767                } elseif ( $q['post__not_in'] ) {
    27772768                        $post__not_in = implode(',',  array_map( 'absint', $q['post__not_in'] ));
    2778                         $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
     2769                        $where .= " AND {$this->db->posts}.ID NOT IN ($post__not_in)";
    27792770                }
    27802771
    27812772                if ( is_numeric( $q['post_parent'] ) ) {
    2782                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
     2773                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_parent = %d ", $q['post_parent'] );
    27832774                } elseif ( $q['post_parent__in'] ) {
    27842775                        $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
    2785                         $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
     2776                        $where .= " AND {$this->db->posts}.post_parent IN ($post_parent__in)";
    27862777                } elseif ( $q['post_parent__not_in'] ) {
    27872778                        $post_parent__not_in = implode( ',',  array_map( 'absint', $q['post_parent__not_in'] ) );
    2788                         $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
     2779                        $where .= " AND {$this->db->posts}.post_parent NOT IN ($post_parent__not_in)";
    27892780                }
    27902781
    27912782                if ( $q['page_id'] ) {
    27922783                        if  ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
    27932784                                $q['p'] = $q['page_id'];
    2794                                 $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
     2785                                $where = " AND {$this->db->posts}.ID = " . $q['page_id'];
    27952786                        }
    27962787                }
    27972788
     
    28162807                if ( !$this->is_singular ) {
    28172808                        $this->parse_tax_query( $q );
    28182809
    2819                         $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
     2810                        $clauses = $this->tax_query->get_sql( $this->db->posts, 'ID' );
    28202811
    28212812                        $join .= $clauses['join'];
    28222813                        $where .= $clauses['where'];
     
    29002891                }
    29012892
    29022893                if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
    2903                         $groupby = "{$wpdb->posts}.ID";
     2894                        $groupby = "{$this->db->posts}.ID";
    29042895                }
    29052896
    29062897                // Author/user stuff
     
    29172908
    29182909                if ( ! empty( $q['author__not_in'] ) ) {
    29192910                        $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
    2920                         $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
     2911                        $where .= " AND {$this->db->posts}.post_author NOT IN ($author__not_in) ";
    29212912                } elseif ( ! empty( $q['author__in'] ) ) {
    29222913                        $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
    2923                         $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
     2914                        $where .= " AND {$this->db->posts}.post_author IN ($author__in) ";
    29242915                }
    29252916
    29262917                // Author stuff for nice URLs
     
    29382929                        $q['author'] = get_user_by('slug', $q['author_name']);
    29392930                        if ( $q['author'] )
    29402931                                $q['author'] = $q['author']->ID;
    2941                         $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
     2932                        $whichauthor .= " AND ({$this->db->posts}.post_author = " . absint($q['author']) . ')';
    29422933                }
    29432934
    29442935                // MIME-Type stuff for attachment browsing
    29452936
    29462937                if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
    2947                         $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
     2938                        $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $this->db->posts );
    29482939
    29492940                $where .= $search . $whichauthor . $whichmimetype;
    29502941
    29512942                if ( ! empty( $this->meta_query->queries ) ) {
    2952                         $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
     2943                        $clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this );
    29532944                        $join   .= $clauses['join'];
    29542945                        $where  .= $clauses['where'];
    29552946                }
     
    29702961                        if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
    29712962                                $orderby = '';
    29722963                        } else {
    2973                                 $orderby = "$wpdb->posts.post_date " . $q['order'];
     2964                                $orderby = "{$this->db->posts}.post_date " . $q['order'];
    29742965                        }
    29752966                } elseif ( 'none' == $q['orderby'] ) {
    29762967                        $orderby = '';
    29772968                } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
    2978                         $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
     2969                        $orderby = "FIELD( {$this->db->posts}.ID, $post__in )";
    29792970                } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
    2980                         $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
     2971                        $orderby = "FIELD( {$this->db->posts}.post_parent, $post_parent__in )";
    29812972                } elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
    2982                         $orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
     2973                        $orderby = "FIELD( {$this->db->posts}.post_name, $post_name__in )";
    29832974                } else {
    29842975                        $orderby_array = array();
    29852976                        if ( is_array( $q['orderby'] ) ) {
     
    30113002                                $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
    30123003
    30133004                                if ( empty( $orderby ) ) {
    3014                                         $orderby = "$wpdb->posts.post_date " . $q['order'];
     3005                                        $orderby = "{$this->db->posts}.post_date " . $q['order'];
    30153006                                } elseif ( ! empty( $q['order'] ) ) {
    30163007                                        $orderby .= " {$q['order']}";
    30173008                                }
     
    30513042                }
    30523043
    30533044                if ( isset( $q['post_password'] ) ) {
    3054                         $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
     3045                        $where .= $this->db->prepare( " AND {$this->db->posts}.post_password = %s", $q['post_password'] );
    30553046                        if ( empty( $q['perm'] ) ) {
    30563047                                $q['perm'] = 'readable';
    30573048                        }
    30583049                } elseif ( isset( $q['has_password'] ) ) {
    3059                         $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
     3050                        $where .= sprintf( " AND {$this->db->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' );
    30603051                }
    30613052
    30623053                if ( ! empty( $q['comment_status'] ) ) {
    3063                         $where .= $wpdb->prepare( " AND $wpdb->posts.comment_status = %s ", $q['comment_status'] );
     3054                        $where .= $this->db->prepare( " AND {$this->db->posts}.comment_status = %s ", $q['comment_status'] );
    30643055                }
    30653056
    30663057                if ( ! empty( $q['ping_status'] ) )  {
    3067                         $where .= $wpdb->prepare( " AND $wpdb->posts.ping_status = %s ", $q['ping_status'] );
     3058                        $where .= $this->db->prepare( " AND {$this->db->posts}.ping_status = %s ", $q['ping_status'] );
    30683059                }
    30693060
    30703061                if ( 'any' == $post_type ) {
     
    30723063                        if ( empty( $in_search_post_types ) )
    30733064                                $where .= ' AND 1=0 ';
    30743065                        else
    3075                                 $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
     3066                                $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
    30763067                } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
    3077                         $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
     3068                        $where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $post_type) . "')";
    30783069                } elseif ( ! empty( $post_type ) ) {
    3079                         $where .= " AND $wpdb->posts.post_type = '$post_type'";
     3070                        $where .= " AND {$this->db->posts}.post_type = '$post_type'";
    30803071                        $post_type_object = get_post_type_object ( $post_type );
    30813072                } elseif ( $this->is_attachment ) {
    3082                         $where .= " AND $wpdb->posts.post_type = 'attachment'";
     3073                        $where .= " AND {$this->db->posts}.post_type = 'attachment'";
    30833074                        $post_type_object = get_post_type_object ( 'attachment' );
    30843075                } elseif ( $this->is_page ) {
    3085                         $where .= " AND $wpdb->posts.post_type = 'page'";
     3076                        $where .= " AND {$this->db->posts}.post_type = 'page'";
    30863077                        $post_type_object = get_post_type_object ( 'page' );
    30873078                } else {
    3088                         $where .= " AND $wpdb->posts.post_type = 'post'";
     3079                        $where .= " AND {$this->db->posts}.post_type = 'post'";
    30893080                        $post_type_object = get_post_type_object ( 'post' );
    30903081                }
    30913082
     
    31143105                        if ( in_array( 'any', $q_status ) ) {
    31153106                                foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
    31163107                                        if ( ! in_array( $status, $q_status ) ) {
    3117                                                 $e_status[] = "$wpdb->posts.post_status <> '$status'";
     3108                                                $e_status[] = "{$this->db->posts}.post_status <> '$status'";
    31183109                                        }
    31193110                                }
    31203111                        } else {
     
    31213112                                foreach ( get_post_stati() as $status ) {
    31223113                                        if ( in_array( $status, $q_status ) ) {
    31233114                                                if ( 'private' == $status )
    3124                                                         $p_status[] = "$wpdb->posts.post_status = '$status'";
     3115                                                        $p_status[] = "{$this->db->posts}.post_status = '$status'";
    31253116                                                else
    3126                                                         $r_status[] = "$wpdb->posts.post_status = '$status'";
     3117                                                        $r_status[] = "{$this->db->posts}.post_status = '$status'";
    31273118                                        }
    31283119                                }
    31293120                        }
     
    31383129                        }
    31393130                        if ( !empty($r_status) ) {
    31403131                                if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
    3141                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
     3132                                        $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
    31423133                                else
    31433134                                        $statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
    31443135                        }
    31453136                        if ( !empty($p_status) ) {
    31463137                                if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) )
    3147                                         $statuswheres[] = "($wpdb->posts.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
     3138                                        $statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
    31483139                                else
    31493140                                        $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
    31503141                        }
    31513142                        if ( $post_status_join ) {
    3152                                 $join .= " LEFT JOIN $wpdb->posts AS p2 ON ($wpdb->posts.post_parent = p2.ID) ";
     3143                                $join .= " LEFT JOIN {$this->db->posts} AS p2 ON ({$this->db->posts}.post_parent = p2.ID) ";
    31533144                                foreach ( $statuswheres as $index => $statuswhere )
    3154                                         $statuswheres[$index] = "($statuswhere OR ($wpdb->posts.post_status = 'inherit' AND " . str_replace($wpdb->posts, 'p2', $statuswhere) . "))";
     3145                                        $statuswheres[$index] = "($statuswhere OR ({$this->db->posts}.post_status = 'inherit' AND " . str_replace( $this->db->posts, 'p2', $statuswhere ) . "))";
    31553146                        }
    31563147                        $where_status = implode( ' OR ', $statuswheres );
    31573148                        if ( ! empty( $where_status ) ) {
     
    31583149                                $where .= " AND ($where_status)";
    31593150                        }
    31603151                } elseif ( !$this->is_singular ) {
    3161                         $where .= " AND ($wpdb->posts.post_status = 'publish'";
     3152                        $where .= " AND ({$this->db->posts}.post_status = 'publish'";
    31623153
    31633154                        // Add public states.
    31643155                        $public_states = get_post_stati( array('public' => true) );
     
    31653156                        foreach ( (array) $public_states as $state ) {
    31663157                                if ( 'publish' == $state ) // Publish is hard-coded above.
    31673158                                        continue;
    3168                                 $where .= " OR $wpdb->posts.post_status = '$state'";
     3159                                $where .= " OR {$this->db->posts}.post_status = '$state'";
    31693160                        }
    31703161
    31713162                        if ( $this->is_admin ) {
     
    31723163                                // Add protected states that should show in the admin all list.
    31733164                                $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
    31743165                                foreach ( (array) $admin_all_states as $state )
    3175                                         $where .= " OR $wpdb->posts.post_status = '$state'";
     3166                                        $where .= " OR {$this->db->posts}.post_status = '$state'";
    31763167                        }
    31773168
    31783169                        if ( is_user_logged_in() ) {
     
    31793170                                // Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
    31803171                                $private_states = get_post_stati( array('private' => true) );
    31813172                                foreach ( (array) $private_states as $state )
    3182                                         $where .= current_user_can( $read_private_cap ) ? " OR $wpdb->posts.post_status = '$state'" : " OR $wpdb->posts.post_author = $user_id AND $wpdb->posts.post_status = '$state'";
     3173                                        $where .= current_user_can( $read_private_cap ) ? " OR {$this->db->posts}.post_status = '$state'" : " OR {$this->db->posts}.post_author = $user_id AND {$this->db->posts}.post_status = '$state'";
    31833174                        }
    31843175
    31853176                        $where .= ')';
     
    32303221                // Comments feeds
    32313222                if ( $this->is_comment_feed && ! $this->is_singular ) {
    32323223                        if ( $this->is_archive || $this->is_search ) {
    3233                                 $cjoin = "JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID) $join ";
     3224                                $cjoin = "JOIN {$this->db->posts} ON ({$this->db->comments}.comment_post_ID = {$this->db->posts}.ID) $join ";
    32343225                                $cwhere = "WHERE comment_approved = '1' $where";
    3235                                 $cgroupby = "$wpdb->comments.comment_id";
     3226                                $cgroupby = "{$this->db->comments}.comment_id";
    32363227                        } else { // Other non singular e.g. front
    3237                                 $cjoin = "JOIN $wpdb->posts ON ( $wpdb->comments.comment_post_ID = $wpdb->posts.ID )";
     3228                                $cjoin = "JOIN {$this->db->posts} ON ( {$this->db->comments}.comment_post_ID = {$this->db->posts}.ID )";
    32383229                                $cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' && post_type = 'attachment' ) ) AND comment_approved = '1'";
    32393230                                $cgroupby = '';
    32403231                        }
     
    32933284                        $cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
    32943285                        $corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
    32953286
    3296                         $comments = (array) $wpdb->get_results("SELECT $distinct $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits");
     3287                        $comments = (array) $this->db->get_results("SELECT $distinct {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits");
    32973288                        // Convert to WP_Comment
    32983289                        $this->comments = array_map( 'get_comment', $comments );
    32993290                        $this->comment_count = count($this->comments);
     
    33063297                        $post_ids = join(',', $post_ids);
    33073298                        $join = '';
    33083299                        if ( $post_ids )
    3309                                 $where = "AND $wpdb->posts.ID IN ($post_ids) ";
     3300                                $where = "AND {$this->db->posts}.ID IN ($post_ids) ";
    33103301                        else
    33113302                                $where = "AND 0";
    33123303                }
     
    35473538                if ( !$q['no_found_rows'] && !empty($limits) )
    35483539                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    35493540
    3550                 $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     3541                $this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
    35513542
    35523543                if ( !$q['suppress_filters'] ) {
    35533544                        /**
     
    35813572
    35823573                if ( 'ids' == $q['fields'] ) {
    35833574                        if ( null === $this->posts ) {
    3584                                 $this->posts = $wpdb->get_col( $this->request );
     3575                                $this->posts = $this->db->get_col( $this->request );
    35853576                        }
    35863577
    35873578                        $this->posts = array_map( 'intval', $this->posts );
     
    35933584
    35943585                if ( 'id=>parent' == $q['fields'] ) {
    35953586                        if ( null === $this->posts ) {
    3596                                 $this->posts = $wpdb->get_results( $this->request );
     3587                                $this->posts = $this->db->get_results( $this->request );
    35973588                        }
    35983589
    35993590                        $this->post_count = count( $this->posts );
     
    36113602                }
    36123603
    36133604                if ( null === $this->posts ) {
    3614                         $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
     3605                        $split_the_query = ( $old_request == $this->request && "{$this->db->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
    36153606
    36163607                        /**
    36173608                         * Filters whether to split the query.
     
    36303621                        if ( $split_the_query ) {
    36313622                                // First get the IDs and then fill in the objects
    36323623
    3633                                 $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     3624                                $this->request = "SELECT $found_rows $distinct {$this->db->posts}.ID FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
    36343625
    36353626                                /**
    36363627                                 * Filters the Post IDs SQL request before sending.
     
    36423633                                 */
    36433634                                $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
    36443635
    3645                                 $ids = $wpdb->get_col( $this->request );
     3636                                $ids = $this->db->get_col( $this->request );
    36463637
    36473638                                if ( $ids ) {
    36483639                                        $this->posts = $ids;
     
    36523643                                        $this->posts = array();
    36533644                                }
    36543645                        } else {
    3655                                 $this->posts = $wpdb->get_results( $this->request );
     3646                                $this->posts = $this->db->get_results( $this->request );
    36563647                                $this->set_found_posts( $q, $limits );
    36573648                        }
    36583649                }
     
    36923683                        /** This filter is documented in wp-includes/query.php */
    36933684                        $climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
    36943685
    3695                         $comments_request = "SELECT $wpdb->comments.* FROM $wpdb->comments $cjoin $cwhere $cgroupby $corderby $climits";
    3696                         $comments = $wpdb->get_results($comments_request);
     3686                        $comments_request = "SELECT {$this->db->comments}.* FROM $this->db->comments $cjoin $cwhere $cgroupby $corderby $climits";
     3687                        $comments = $this->db->get_results($comments_request);
    36973688                        // Convert to WP_Comment
    36983689                        $this->comments = array_map( 'get_comment', $comments );
    36993690                        $this->comment_count = count($this->comments);
     
    38363827         * @since 3.5.0
    38373828         * @access private
    38383829         *
    3839          * @global wpdb $wpdb WordPress database abstraction object.
    3840          *
    38413830         * @param array  $q      Query variables.
    38423831         * @param string $limits LIMIT clauses of the query.
    38433832         */
    38443833        private function set_found_posts( $q, $limits ) {
    3845                 global $wpdb;
    3846 
    38473834                // Bail if posts is an empty array. Continue if posts is an empty string,
    38483835                // null, or false to accommodate caching plugins that fill posts later.
    38493836                if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
     
    38583845                         * @param string   $found_posts The query to run to find the found posts.
    38593846                         * @param WP_Query &$this       The WP_Query instance (passed by reference).
    38603847                         */
    3861                         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     3848                        $this->found_posts = $this->db->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
    38623849                } else {
    38633850                        $this->found_posts = count( $this->posts );
    38643851                }
     
    41564143         *
    41574144         * @param string|array $query URL query string or array of vars.
    41584145         */
    4159         public function __construct($query = '') {
    4160                 if ( ! empty($query) ) {
    4161                         $this->query($query);
     4146        public function __construct( $query = '' ) {
     4147                $this->db = $GLOBALS['wpdb'];
     4148
     4149                if ( ! empty( $query ) ) {
     4150                        $this->query( $query );
    41624151                }
    41634152        }
    41644153