+ if ( !empty($q['tag_id']) ) {
+ $q['tag_id'] = absint( $q['tag_id'] );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag_id']
+ );
+ }
+
+ if ( !empty($q['tag__in']) ) {
+ $q['tag__in'] = array_map('absint', array_unique( (array) $q['tag__in'] ) );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag__in']
+ );
+ }
+
+ if ( !empty($q['tag__not_in']) ) {
+ $q['tag__not_in'] = array_map('absint', array_unique( (array) $q['tag__not_in'] ) );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag__not_in'],
+ 'operator' => 'NOT IN'
+ );
+ }
+
+ if ( !empty($q['tag__and']) ) {
+ $q['tag__and'] = array_map('absint', array_unique( (array) $q['tag__and'] ) );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag__and'],
+ 'operator' => 'AND'
+ );
+ }
+
+ if ( !empty($q['tag_slug__in']) ) {
+ $q['tag_slug__in'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__in'] ) );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag_slug__in'],
+ 'field' => 'slug'
+ );
+ }
+
+ if ( !empty($q['tag_slug__and']) ) {
+ $q['tag_slug__and'] = array_map('sanitize_title_for_query', array_unique( (array) $q['tag_slug__and'] ) );
+ $tax_query[] = array(
+ 'taxonomy' => 'post_tag',
+ 'terms' => $q['tag_slug__and'],
+ 'field' => 'slug',
+ 'operator' => 'AND'
+ );
+ }
+
+ $this->tax_query = new WP_Tax_Query( $tax_query );
+
+ /**
+ * Fires after taxonomy-related query vars have been parsed.
+ *
+ * @since 3.7.0
+ *
+ * @param WP_Query $this The WP_Query instance.
+ */
+ do_action( 'parse_tax_query', $this );
+ }
+
+ /**
+ * Generate SQL for the WHERE clause based on passed search terms.
+ *
+ * @since 3.7.0
+ *
+ * @global wpdb $wpdb
+ * @param array $q Query variables.
+ * @return string WHERE clause.
+ */
+ protected function parse_search( &$q ) {
+ global $wpdb;
+
+ $search = '';
+
+ // added slashes screw with quote grouping when done early, so done later
+ $q['s'] = stripslashes( $q['s'] );
+ if ( empty( $_GET['s'] ) && $this->is_main_query() )
+ $q['s'] = urldecode( $q['s'] );
+ // there are no line breaks in <input /> fields
+ $q['s'] = str_replace( array( "\r", "\n" ), '', $q['s'] );
+ $q['search_terms_count'] = 1;
+ if ( ! empty( $q['sentence'] ) ) {
+ $q['search_terms'] = array( $q['s'] );
+ } else {
+ if ( preg_match_all( '/".*?("|$)|((?<=[\t ",+])|^)[^\t ",+]+/', $q['s'], $matches ) ) {
+ $q['search_terms_count'] = count( $matches[0] );
+ $q['search_terms'] = $this->parse_search_terms( $matches[0] );
+ // if the search string has only short terms or stopwords, or is 10+ terms long, match it as sentence
+ if ( empty( $q['search_terms'] ) || count( $q['search_terms'] ) > 9 )
+ $q['search_terms'] = array( $q['s'] );
+ } else {
+ $q['search_terms'] = array( $q['s'] );
+ }
+ }
+
+ $n = ! empty( $q['exact'] ) ? '' : '%';
+ $searchand = '';
+ $q['search_orderby_title'] = array();
+ foreach ( $q['search_terms'] as $term ) {
+ if ( $n ) {
+ $like = '%' . $wpdb->esc_like( $term ) . '%';
+ $q['search_orderby_title'][] = $wpdb->prepare( "$wpdb->posts.post_title LIKE %s", $like );
+ }
+
+ $like = $n . $wpdb->esc_like( $term ) . $n;
+ $search .= $wpdb->prepare( "{$searchand}(($wpdb->posts.post_title LIKE %s) OR ($wpdb->posts.post_content LIKE %s))", $like, $like );
+ $searchand = ' AND ';
+ }
+
+ if ( ! empty( $search ) ) {
+ $search = " AND ({$search}) ";
+ if ( ! is_user_logged_in() )
+ $search .= " AND ($wpdb->posts.post_password = '') ";
+ }
+
+ return $search;
+ }
+
+ /**
+ * Check if the terms are suitable for searching.
+ *
+ * Uses an array of stopwords (terms) that are excluded from the separate
+ * term matching when searching for posts. The list of English stopwords is
+ * the approximate search engines list, and is translatable.
+ *
+ * @since 3.7.0
+ *
+ * @param array Terms to check.
+ * @return array Terms that are not stopwords.
+ */
+ protected function parse_search_terms( $terms ) {
+ $strtolower = function_exists( 'mb_strtolower' ) ? 'mb_strtolower' : 'strtolower';
+ $checked = array();
+
+ $stopwords = $this->get_search_stopwords();
+
+ foreach ( $terms as $term ) {
+ // keep before/after spaces when term is for exact match
+ if ( preg_match( '/^".+"$/', $term ) )
+ $term = trim( $term, "\"'" );
+ else
+ $term = trim( $term, "\"' " );
+
+ // Avoid single A-Z.
+ if ( ! $term || ( 1 === strlen( $term ) && preg_match( '/^[a-z]$/i', $term ) ) )
+ continue;
+
+ if ( in_array( call_user_func( $strtolower, $term ), $stopwords, true ) )
+ continue;
+
+ $checked[] = $term;
+ }
+
+ return $checked;
+ }
+
+ /**
+ * Retrieve stopwords used when parsing search terms.
+ *
+ * @since 3.7.0
+ *
+ * @return array Stopwords.
+ */
+ protected function get_search_stopwords() {
+ if ( isset( $this->stopwords ) )
+ return $this->stopwords;
+
+ /* translators: This is a comma-separated list of very common words that should be excluded from a search,
+ * like a, an, and the. These are usually called "stopwords". You should not simply translate these individual
+ * words into your language. Instead, look for and provide commonly accepted stopwords in your language.
+ */
+ $words = explode( ',', _x( 'about,an,are,as,at,be,by,com,for,from,how,in,is,it,of,on,or,that,the,this,to,was,what,when,where,who,will,with,www',
+ 'Comma-separated list of search stopwords in your language' ) );
+
+ $stopwords = array();
+ foreach( $words as $word ) {
+ $word = trim( $word, "\r\n\t " );
+ if ( $word )
+ $stopwords[] = $word;
+ }
+
+ /**
+ * Filter stopwords used when parsing search terms.
+ *
+ * @since 3.7.0
+ *
+ * @param array $stopwords Stopwords.
+ */
+ $this->stopwords = apply_filters( 'wp_search_stopwords', $stopwords );
+ return $this->stopwords;
+ }
+
+ /**
+ * Generate SQL for the ORDER BY condition based on passed search terms.
+ *
+ * @global wpdb $wpdb
+ * @param array $q Query variables.
+ * @return string ORDER BY clause.
+ */
+ protected function parse_search_order( &$q ) {
+ global $wpdb;
+
+ if ( $q['search_terms_count'] > 1 ) {
+ $num_terms = count( $q['search_orderby_title'] );
+ $like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
+
+ $search_orderby = '(CASE ';
+ // sentence match in 'post_title'
+ $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_title LIKE %s THEN 1 ", $like );
+
+ // sanity limit, sort as sentence when more than 6 terms
+ // (few searches are longer than 6 terms and most titles are not)
+ if ( $num_terms < 7 ) {
+ // all words in title
+ $search_orderby .= 'WHEN ' . implode( ' AND ', $q['search_orderby_title'] ) . ' THEN 2 ';
+ // any word in title, not needed when $num_terms == 1
+ if ( $num_terms > 1 )
+ $search_orderby .= 'WHEN ' . implode( ' OR ', $q['search_orderby_title'] ) . ' THEN 3 ';
+ }
+
+ // sentence match in 'post_content'
+ $search_orderby .= $wpdb->prepare( "WHEN $wpdb->posts.post_content LIKE %s THEN 4 ", $like );
+ $search_orderby .= 'ELSE 5 END)';
+ } else {
+ // single word or sentence search
+ $search_orderby = reset( $q['search_orderby_title'] ) . ' DESC';
+ }
+
+ return $search_orderby;
+ }
+
+ /**
+ * If the passed orderby value is allowed, convert the alias to a
+ * properly-prefixed orderby value.
+ *
+ * @since 4.0.0
+ * @access protected
+ *
+ * @global wpdb $wpdb WordPress database access abstraction object.
+ *
+ * @param string $orderby Alias for the field to order by.
+ * @return string|bool Table-prefixed value to used in the ORDER clause. False otherwise.
+ */
+ protected function parse_orderby( $orderby ) {
+ global $wpdb;
+
+ // Used to filter values.
+ $allowed_keys = array(
+ 'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
+ 'post_parent', 'post_type', 'name', 'author', 'date', 'title', 'modified',
+ 'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
+ );
+
+ $meta_key = $this->get( 'meta_key' );
+ if ( ! empty( $meta_key ) ) {
+ $allowed_keys[] = $meta_key;
+ $allowed_keys[] = 'meta_value';
+ $allowed_keys[] = 'meta_value_num';
+ }
+
+ if ( ! in_array( $orderby, $allowed_keys ) ) {
+ return false;
+ }
+
+ switch ( $orderby ) {
+ case 'post_name':
+ case 'post_author':
+ case 'post_date':
+ case 'post_title':
+ case 'post_modified':
+ case 'post_parent':
+ case 'post_type':
+ case 'ID':
+ case 'menu_order':
+ case 'comment_count':
+ $orderby = "$wpdb->posts.{$orderby}";
+ break;
+ case 'rand':
+ $orderby = 'RAND()';
+ break;
+ case $meta_key:
+ case 'meta_value':
+ $type = $this->get( 'meta_type' );
+ if ( ! empty( $type ) ) {
+ $meta_type = $this->meta_query->get_cast_for_type( $type );
+ $orderby = "CAST($wpdb->postmeta.meta_value AS {$meta_type})";
+ } else {
+ $orderby = "$wpdb->postmeta.meta_value";
+ }
+ break;
+ case 'meta_value_num':
+ $orderby = "$wpdb->postmeta.meta_value+0";
+ break;
+ default:
+ $orderby = "$wpdb->posts.post_" . $orderby;
+ break;
+ }
+
+ return $orderby;
+ }
+
+ /**
+ * Parse an 'order' query variable and cast it to ASC or DESC as necessary.
+ *
+ * @since 4.0.0
+ * @access protected
+ *
+ * @param string $order The 'order' query variable.
+ * @return string The sanitized 'order' query variable.
+ */
+ protected function parse_order( $order ) {
+ if ( ! is_string( $order ) || empty( $order ) ) {
+ return 'DESC';
+ }
+
+ if ( 'ASC' === strtoupper( $order ) ) {
+ return 'ASC';
+ } else {
+ return 'DESC';
+ }
+ }
+
+ /**
+ * Sets the 404 property and saves whether query is feed.
+ *
+ * @since 2.0.0
+ * @access public
+ */
+ public function set_404() {
+ $is_feed = $this->is_feed;
+
+ $this->init_query_flags();
+ $this->is_404 = true;
+
+ $this->is_feed = $is_feed;
+ }
+
+ /**
+ * Retrieve query variable.
+ *
+ * @since 1.5.0
+ * @access public
+ *
+ * @param string $query_var Query variable key.
+ * @param mixed $default Value to return if the query variable is not set. Default ''.
+ * @return mixed
+ */
+ public function get( $query_var, $default = '' ) {
+ if ( isset( $this->query_vars[ $query_var ] ) ) {
+ return $this->query_vars[ $query_var ];
+ }
+
+ return $default;
+ }
+
+ /**
+ * Set query variable.
+ *
+ * @since 1.5.0
+ * @access public
+ *
+ * @param string $query_var Query variable key.
+ * @param mixed $value Query variable value.
+ */
+ public function set($query_var, $value) {
+ $this->query_vars[$query_var] = $value;
+ }
+
+ /**
+ * Retrieve the posts based on query variables.
+ *
+ * There are a few filters and actions that can be used to modify the post
+ * database query.
+ *
+ * @since 1.5.0
+ * @access public
+ * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
+ *
+ * @return array List of posts.
+ */
+ public function get_posts() {
+ global $wpdb;
+
+ $this->parse_query();
+
+ /**
+ * Fires after the query variable object is created, but before the actual query is run.
+ *
+ * Note: If using conditional tags, use the method versions within the passed instance
+ * (e.g. $this->is_main_query() instead of is_main_query()). This is because the functions
+ * like is_main_query() test against the global $wp_query instance, not the passed one.
+ *
+ * @since 2.0.0
+ *
+ * @param WP_Query &$this The WP_Query instance (passed by reference).
+ */
+ do_action_ref_array( 'pre_get_posts', array( &$this ) );
+
+ // Shorthand.
+ $q = &$this->query_vars;
+
+ // Fill again in case pre_get_posts unset some vars.
+ $q = $this->fill_query_vars($q);
+
+ // Parse meta query
+ $this->meta_query = new WP_Meta_Query();
+ $this->meta_query->parse_query_vars( $q );
+
+ // Set a flag if a pre_get_posts hook changed the query vars.
+ $hash = md5( serialize( $this->query_vars ) );
+ if ( $hash != $this->query_vars_hash ) {
+ $this->query_vars_changed = true;
+ $this->query_vars_hash = $hash;
+ }
+ unset($hash);
+
+ // First let's clear some variables
+ $distinct = '';
+ $whichauthor = '';
+ $whichmimetype = '';
+ $where = '';
+ $limits = '';
+ $join = '';
+ $search = '';
+ $groupby = '';
+ $post_status_join = false;
+ $page = 1;
+
+ if ( isset( $q['caller_get_posts'] ) ) {
+ _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
+ if ( !isset( $q['ignore_sticky_posts'] ) )
+ $q['ignore_sticky_posts'] = $q['caller_get_posts'];
+ }
+
+ if ( !isset( $q['ignore_sticky_posts'] ) )
+ $q['ignore_sticky_posts'] = false;
+
+ if ( !isset($q['suppress_filters']) )
+ $q['suppress_filters'] = false;
+
+ if ( !isset($q['cache_results']) ) {
+ if ( wp_using_ext_object_cache() )
+ $q['cache_results'] = false;
+ else
+ $q['cache_results'] = true;
+ }
+
+ if ( !isset($q['update_post_term_cache']) )
+ $q['update_post_term_cache'] = true;
+
+ if ( !isset($q['update_post_meta_cache']) )
+ $q['update_post_meta_cache'] = true;
+
+ if ( !isset($q['post_type']) ) {
+ if ( $this->is_search )
+ $q['post_type'] = 'any';
+ else
+ $q['post_type'] = '';
+ }
+ $post_type = $q['post_type'];
+ if ( empty( $q['posts_per_page'] ) ) {
+ $q['posts_per_page'] = get_option( 'posts_per_page' );
+ }
+ if ( isset($q['showposts']) && $q['showposts'] ) {
+ $q['showposts'] = (int) $q['showposts'];
+ $q['posts_per_page'] = $q['showposts'];
+ }
+ if ( (isset($q['posts_per_archive_page']) && $q['posts_per_archive_page'] != 0) && ($this->is_archive || $this->is_search) )
+ $q['posts_per_page'] = $q['posts_per_archive_page'];
+ if ( !isset($q['nopaging']) ) {
+ if ( $q['posts_per_page'] == -1 ) {
+ $q['nopaging'] = true;
+ } else {
+ $q['nopaging'] = false;
+ }
+ }
+
+ if ( $this->is_feed ) {
+ // This overrides posts_per_page.
+ if ( ! empty( $q['posts_per_rss'] ) ) {
+ $q['posts_per_page'] = $q['posts_per_rss'];
+ } else {
+ $q['posts_per_page'] = get_option( 'posts_per_rss' );
+ }
+ $q['nopaging'] = false;
+ }
+ $q['posts_per_page'] = (int) $q['posts_per_page'];
+ if ( $q['posts_per_page'] < -1 )
+ $q['posts_per_page'] = abs($q['posts_per_page']);
+ else if ( $q['posts_per_page'] == 0 )
+ $q['posts_per_page'] = 1;
+
+ if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
+ $q['comments_per_page'] = get_option('comments_per_page');
+
+ if ( $this->is_home && (empty($this->query) || $q['preview'] == 'true') && ( 'page' == get_option('show_on_front') ) && get_option('page_on_front') ) {
+ $this->is_page = true;
+ $this->is_home = false;
+ $q['page_id'] = get_option('page_on_front');
+ }
+
+ if ( isset($q['page']) ) {
+ $q['page'] = trim($q['page'], '/');
+ $q['page'] = absint($q['page']);
+ }
+
+ // If true, forcibly turns off SQL_CALC_FOUND_ROWS even when limits are present.
+ if ( isset($q['no_found_rows']) )
+ $q['no_found_rows'] = (bool) $q['no_found_rows'];
+ else
+ $q['no_found_rows'] = false;
+
+ switch ( $q['fields'] ) {
+ case 'ids':
+ $fields = "$wpdb->posts.ID";
+ break;
+ case 'id=>parent':
+ $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
+ break;
+ default:
+ $fields = "$wpdb->posts.*";
+ }
+
+ 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();
+