+ if ( '' !== $q['menu_order'] )
+ $where .= " AND $wpdb->posts.menu_order = " . $q['menu_order'];
+
+ // The "m" parameter is meant for months but accepts datetimes of varying specificity
+ if ( $q['m'] ) {
+ $where .= " AND YEAR($wpdb->posts.post_date)=" . substr($q['m'], 0, 4);
+ if ( strlen($q['m']) > 5 )
+ $where .= " AND MONTH($wpdb->posts.post_date)=" . substr($q['m'], 4, 2);
+ if ( strlen($q['m']) > 7 )
+ $where .= " AND DAYOFMONTH($wpdb->posts.post_date)=" . substr($q['m'], 6, 2);
+ if ( strlen($q['m']) > 9 )
+ $where .= " AND HOUR($wpdb->posts.post_date)=" . substr($q['m'], 8, 2);
+ if ( strlen($q['m']) > 11 )
+ $where .= " AND MINUTE($wpdb->posts.post_date)=" . substr($q['m'], 10, 2);
+ if ( strlen($q['m']) > 13 )
+ $where .= " AND SECOND($wpdb->posts.post_date)=" . substr($q['m'], 12, 2);
+ }
+
+ // Handle the other individual date parameters
+ $date_parameters = array();
+
+ if ( '' !== $q['hour'] )
+ $date_parameters['hour'] = $q['hour'];
+
+ if ( '' !== $q['minute'] )
+ $date_parameters['minute'] = $q['minute'];
+
+ if ( '' !== $q['second'] )
+ $date_parameters['second'] = $q['second'];
+
+ if ( $q['year'] )
+ $date_parameters['year'] = $q['year'];
+
+ if ( $q['monthnum'] )
+ $date_parameters['monthnum'] = $q['monthnum'];
+
+ if ( $q['w'] )
+ $date_parameters['week'] = $q['w'];
+
+ if ( $q['day'] )
+ $date_parameters['day'] = $q['day'];
+
+ if ( $date_parameters ) {
+ $date_query = new WP_Date_Query( array( $date_parameters ) );
+ $where .= $date_query->get_sql();
+ }
+ unset( $date_parameters, $date_query );
+
+ // Handle complex date queries
+ if ( ! empty( $q['date_query'] ) ) {
+ $this->date_query = new WP_Date_Query( $q['date_query'] );
+ $where .= $this->date_query->get_sql();
+ }
+
+
+ // If we've got a post_type AND it's not "any" post_type.
+ if ( !empty($q['post_type']) && 'any' != $q['post_type'] ) {
+ foreach ( (array)$q['post_type'] as $_post_type ) {
+ $ptype_obj = get_post_type_object($_post_type);
+ if ( !$ptype_obj || !$ptype_obj->query_var || empty($q[ $ptype_obj->query_var ]) )
+ continue;
+
+ if ( ! $ptype_obj->hierarchical ) {
+ // Non-hierarchical post types can directly use 'name'.
+ $q['name'] = $q[ $ptype_obj->query_var ];
+ } else {
+ // Hierarchical post types will operate through 'pagename'.
+ $q['pagename'] = $q[ $ptype_obj->query_var ];
+ $q['name'] = '';
+ }
+
+ // Only one request for a slug is possible, this is why name & pagename are overwritten above.
+ break;
+ } //end foreach
+ unset($ptype_obj);
+ }
+
+ if ( '' != $q['name'] ) {
+ $q['name'] = sanitize_title_for_query( $q['name'] );
+ $where .= " AND $wpdb->posts.post_name = '" . $q['name'] . "'";
+ } elseif ( '' != $q['pagename'] ) {
+ if ( isset($this->queried_object_id) ) {
+ $reqpage = $this->queried_object_id;
+ } else {
+ if ( 'page' != $q['post_type'] ) {
+ foreach ( (array)$q['post_type'] as $_post_type ) {
+ $ptype_obj = get_post_type_object($_post_type);
+ if ( !$ptype_obj || !$ptype_obj->hierarchical )
+ continue;
+
+ $reqpage = get_page_by_path($q['pagename'], OBJECT, $_post_type);
+ if ( $reqpage )
+ break;
+ }
+ unset($ptype_obj);
+ } else {
+ $reqpage = get_page_by_path($q['pagename']);
+ }
+ if ( !empty($reqpage) )
+ $reqpage = $reqpage->ID;
+ else
+ $reqpage = 0;
+ }
+
+ $page_for_posts = get_option('page_for_posts');
+ if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
+ $q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
+ $q['name'] = $q['pagename'];
+ $where .= " AND ($wpdb->posts.ID = '$reqpage')";
+ $reqpage_obj = get_post( $reqpage );
+ if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
+ $this->is_attachment = true;
+ $post_type = $q['post_type'] = 'attachment';
+ $this->is_page = true;
+ $q['attachment_id'] = $reqpage;
+ }
+ }
+ } elseif ( '' != $q['attachment'] ) {
+ $q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
+ $q['name'] = $q['attachment'];
+ $where .= " AND $wpdb->posts.post_name = '" . $q['attachment'] . "'";
+ }
+
+
+ if ( intval($q['comments_popup']) )
+ $q['p'] = absint($q['comments_popup']);
+
+ // If an attachment is requested by number, let it supersede any post number.
+ if ( $q['attachment_id'] )
+ $q['p'] = absint($q['attachment_id']);
+
+ // If a post number is specified, load that post
+ if ( $q['p'] ) {
+ $where .= " AND {$wpdb->posts}.ID = " . $q['p'];
+ } elseif ( $q['post__in'] ) {
+ $post__in = implode(',', array_map( 'absint', $q['post__in'] ));
+ $where .= " AND {$wpdb->posts}.ID IN ($post__in)";
+ } elseif ( $q['post__not_in'] ) {
+ $post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
+ $where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
+ }
+
+ if ( is_numeric( $q['post_parent'] ) ) {
+ $where .= $wpdb->prepare( " AND $wpdb->posts.post_parent = %d ", $q['post_parent'] );
+ } elseif ( $q['post_parent__in'] ) {
+ $post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
+ $where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
+ } elseif ( $q['post_parent__not_in'] ) {
+ $post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
+ $where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
+ }
+
+ if ( $q['page_id'] ) {
+ if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
+ $q['p'] = $q['page_id'];
+ $where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
+ }
+ }
+
+ // If a search pattern is specified, load the posts that match.
+ if ( ! empty( $q['s'] ) ) {
+ $search = $this->parse_search( $q );
+ }
+
+ /**
+ * Filter the search SQL that is used in the WHERE clause of WP_Query.
+ *
+ * @since 3.0.0
+ *
+ * @param string $search Search SQL for WHERE clause.
+ * @param WP_Query $this The current WP_Query object.
+ */
+ $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
+
+ // Taxonomies
+ if ( !$this->is_singular ) {
+ $this->parse_tax_query( $q );
+
+ $clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
+
+ $join .= $clauses['join'];
+ $where .= $clauses['where'];
+ }
+
+ if ( $this->is_tax ) {
+ if ( empty($post_type) ) {
+ // Do a fully inclusive search for currently registered post types of queried taxonomies
+ $post_type = array();
+ $taxonomies = array_keys( $this->tax_query->queried_terms );
+ foreach ( get_post_types( array( 'exclude_from_search' => false ) ) as $pt ) {
+ $object_taxonomies = $pt === 'attachment' ? get_taxonomies_for_attachments() : get_object_taxonomies( $pt );
+ if ( array_intersect( $taxonomies, $object_taxonomies ) )
+ $post_type[] = $pt;
+ }
+ if ( ! $post_type )
+ $post_type = 'any';
+ elseif ( count( $post_type ) == 1 )
+ $post_type = $post_type[0];
+
+ $post_status_join = true;
+ } elseif ( in_array('attachment', (array) $post_type) ) {
+ $post_status_join = true;
+ }
+ }
+
+ /*
+ * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
+ * 'category_name' vars are set for backward compatibility.
+ */
+ if ( ! empty( $this->tax_query->queried_terms ) ) {
+
+ /*
+ * Set 'taxonomy', 'term', and 'term_id' to the
+ * first taxonomy other than 'post_tag' or 'category'.
+ */
+ if ( ! isset( $q['taxonomy'] ) ) {
+ foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
+ if ( empty( $queried_items['terms'][0] ) ) {
+ continue;
+ }
+
+ if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
+ $q['taxonomy'] = $queried_taxonomy;
+
+ if ( 'slug' === $queried_items['field'] ) {
+ $q['term'] = $queried_items['terms'][0];
+ } else {
+ $q['term_id'] = $queried_items['terms'][0];
+ }
+ }
+ }
+ }
+
+ // 'cat', 'category_name', 'tag_id'
+ foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
+ if ( empty( $queried_items['terms'][0] ) ) {
+ continue;
+ }
+
+ if ( 'category' === $queried_taxonomy ) {
+ $the_cat = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'category' );
+ if ( $the_cat ) {
+ $this->set( 'cat', $the_cat->term_id );
+ $this->set( 'category_name', $the_cat->slug );
+ }
+ unset( $the_cat );
+ }
+
+ if ( 'post_tag' === $queried_taxonomy ) {
+ $the_tag = get_term_by( $queried_items['field'], $queried_items['terms'][0], 'post_tag' );
+ if ( $the_tag ) {
+ $this->set( 'tag_id', $the_tag->term_id );
+ }
+ unset( $the_tag );
+ }
+ }
+ }
+
+ if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
+ $groupby = "{$wpdb->posts}.ID";
+ }
+
+ // Author/user stuff
+
+ if ( ! empty( $q['author'] ) && $q['author'] != '0' ) {
+ $q['author'] = addslashes_gpc( '' . urldecode( $q['author'] ) );
+ $authors = array_unique( array_map( 'intval', preg_split( '/[,\s]+/', $q['author'] ) ) );
+ foreach ( $authors as $author ) {
+ $key = $author > 0 ? 'author__in' : 'author__not_in';
+ $q[$key][] = abs( $author );
+ }
+ $q['author'] = implode( ',', $authors );
+ }
+
+ if ( ! empty( $q['author__not_in'] ) ) {
+ $author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
+ $where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
+ } elseif ( ! empty( $q['author__in'] ) ) {
+ $author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
+ $where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
+ }
+
+ // Author stuff for nice URLs
+
+ if ( '' != $q['author_name'] ) {
+ if ( strpos($q['author_name'], '/') !== false ) {
+ $q['author_name'] = explode('/', $q['author_name']);
+ if ( $q['author_name'][ count($q['author_name'])-1 ] ) {
+ $q['author_name'] = $q['author_name'][count($q['author_name'])-1]; // no trailing slash
+ } else {
+ $q['author_name'] = $q['author_name'][count($q['author_name'])-2]; // there was a trailing slash
+ }
+ }
+ $q['author_name'] = sanitize_title_for_query( $q['author_name'] );
+ $q['author'] = get_user_by('slug', $q['author_name']);
+ if ( $q['author'] )
+ $q['author'] = $q['author']->ID;
+ $whichauthor .= " AND ($wpdb->posts.post_author = " . absint($q['author']) . ')';
+ }
+
+ // MIME-Type stuff for attachment browsing
+
+ if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
+ $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
+
+ $where .= $search . $whichauthor . $whichmimetype;
+
+ if ( ! empty( $this->meta_query->queries ) ) {
+ $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
+ $join .= $clauses['join'];
+ $where .= $clauses['where'];
+ }
+
+ $rand = ( isset( $q['orderby'] ) && 'rand' === $q['orderby'] );
+ if ( ! isset( $q['order'] ) ) {
+ $q['order'] = $rand ? '' : 'DESC';
+ } else {
+ $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
+ }
+
+ // Order by.
+ if ( empty( $q['orderby'] ) ) {
+ /*
+ * Boolean false or empty array blanks out ORDER BY,
+ * while leaving the value unset or otherwise empty sets the default.
+ */
+ if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
+ $orderby = '';
+ } else {
+ $orderby = "$wpdb->posts.post_date " . $q['order'];
+ }
+ } elseif ( 'none' == $q['orderby'] ) {
+ $orderby = '';
+ } elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
+ $orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
+ } elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
+ $orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
+ } else {
+ $orderby_array = array();
+ if ( is_array( $q['orderby'] ) ) {
+ foreach ( $q['orderby'] as $_orderby => $order ) {
+ $orderby = addslashes_gpc( urldecode( $_orderby ) );
+ $parsed = $this->parse_orderby( $orderby );
+
+ if ( ! $parsed ) {
+ continue;
+ }
+
+ $orderby_array[] = $parsed . ' ' . $this->parse_order( $order );
+ }
+ $orderby = implode( ', ', $orderby_array );
+
+ } else {
+ $q['orderby'] = urldecode( $q['orderby'] );
+ $q['orderby'] = addslashes_gpc( $q['orderby'] );
+
+ foreach ( explode( ' ', $q['orderby'] ) as $i => $orderby ) {
+ $parsed = $this->parse_orderby( $orderby );
+ // Only allow certain values for safety.
+ if ( ! $parsed ) {
+ continue;
+ }
+
+ $orderby_array[] = $parsed;
+ }
+ $orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
+
+ if ( empty( $orderby ) ) {
+ $orderby = "$wpdb->posts.post_date " . $q['order'];
+ } elseif ( ! empty( $q['order'] ) ) {
+ $orderby .= " {$q['order']}";
+ }
+ }
+ }
+
+ // Order search results by relevance only when another "orderby" is not specified in the query.
+ if ( ! empty( $q['s'] ) ) {
+ $search_orderby = '';
+ if ( ! empty( $q['search_orderby_title'] ) && ( empty( $q['orderby'] ) && ! $this->is_feed ) || ( isset( $q['orderby'] ) && 'relevance' === $q['orderby'] ) )
+ $search_orderby = $this->parse_search_order( $q );
+
+ /**
+ * Filter the ORDER BY used when ordering search results.
+ *
+ * @since 3.7.0
+ *
+ * @param string $search_orderby The ORDER BY clause.
+ * @param WP_Query $this The current WP_Query instance.
+ */
+ $search_orderby = apply_filters( 'posts_search_orderby', $search_orderby, $this );
+ if ( $search_orderby )
+ $orderby = $orderby ? $search_orderby . ', ' . $orderby : $search_orderby;
+ }
+
+ if ( is_array( $post_type ) && count( $post_type ) > 1 ) {
+ $post_type_cap = 'multiple_post_type';
+ } else {
+ if ( is_array( $post_type ) )
+ $post_type = reset( $post_type );
+ $post_type_object = get_post_type_object( $post_type );
+ if ( empty( $post_type_object ) )
+ $post_type_cap = $post_type;
+ }
+
+ if ( isset( $q['post_password'] ) ) {
+ $where .= $wpdb->prepare( " AND $wpdb->posts.post_password = %s", $q['post_password'] );
+ if ( empty( $q['perm'] ) ) {
+ $q['perm'] = 'readable';
+ }
+ } elseif ( isset( $q['has_password'] ) ) {
+ $where .= sprintf( " AND $wpdb->posts.post_password %s ''", $q['has_password'] ? '!=' : '=' );
+ }
+
+ if ( 'any' == $post_type ) {
+ $in_search_post_types = get_post_types( array('exclude_from_search' => false) );
+ if ( empty( $in_search_post_types ) )
+ $where .= ' AND 1=0 ';
+ else
+ $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
+ } elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
+ $where .= " AND $wpdb->posts.post_type IN ('" . join("', '", $post_type) . "')";
+ } elseif ( ! empty( $post_type ) ) {
+ $where .= " AND $wpdb->posts.post_type = '$post_type'";
+ $post_type_object = get_post_type_object ( $post_type );
+ } elseif ( $this->is_attachment ) {
+ $where .= " AND $wpdb->posts.post_type = 'attachment'";
+ $post_type_object = get_post_type_object ( 'attachment' );
+ } elseif ( $this->is_page ) {
+ $where .= " AND $wpdb->posts.post_type = 'page'";
+ $post_type_object = get_post_type_object ( 'page' );
+ } else {
+ $where .= " AND $wpdb->posts.post_type = 'post'";
+ $post_type_object = get_post_type_object ( 'post' );
+ }
+
+ $edit_cap = 'edit_post';
+ $read_cap = 'read_post';
+
+ if ( ! empty( $post_type_object ) ) {
+ $edit_others_cap = $post_type_object->cap->edit_others_posts;
+ $read_private_cap = $post_type_object->cap->read_private_posts;
+ } else {
+ $edit_others_cap = 'edit_others_' . $post_type_cap . 's';
+ $read_private_cap = 'read_private_' . $post_type_cap . 's';
+ }
+
+ $user_id = get_current_user_id();
+
+ $q_status = array();
+ if ( ! empty( $q['post_status'] ) ) {
+ $statuswheres = array();
+ $q_status = $q['post_status'];
+ if ( ! is_array( $q_status ) )
+ $q_status = explode(',', $q_status);
+ $r_status = array();
+ $p_status = array();
+ $e_status = array();
+ if ( in_array( 'any', $q_status ) ) {
+ foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
+ if ( ! in_array( $status, $q_status ) ) {
+ $e_status[] = "$wpdb->posts.post_status <> '$status'";
+ }
+ }
+ } else {
+ foreach ( get_post_stati() as $status ) {
+ if ( in_array( $status, $q_status ) ) {
+ if ( 'private' == $status )
+ $p_status[] = "$wpdb->posts.post_status = '$status'";
+ else
+ $r_status[] = "$wpdb->posts.post_status = '$status'";
+ }
+ }
+ }
+
+ if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
+ $r_status = array_merge($r_status, $p_status);
+ unset($p_status);
+ }
+
+ if ( !empty($e_status) ) {
+ $statuswheres[] = "(" . join( ' AND ', $e_status ) . ")";
+ }