* The query API attempts to get which part of WordPress the user is on. It
* also provides functionality for getting URL query information.
*
- * @link http://codex.wordpress.org/The_Loop More information on The Loop.
+ * @link https://codex.wordpress.org/The_Loop More information on The Loop.
*
* @package WordPress
* @subpackage Query
/**
* Retrieve variable in the WP_Query class.
*
- * @see WP_Query::get()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param string $var The variable key to retrieve.
* @param mixed $default Value to return if the query variable is not set. Default ''.
*/
function get_query_var( $var, $default = '' ) {
global $wp_query;
-
return $wp_query->get( $var, $default );
}
/**
* Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
*
- * @uses WP_Query::get_queried_object
- *
* @since 3.1.0
* @access public
*
+ * @global WP_Query $wp_query
+ *
* @return object
*/
function get_queried_object() {
/**
* Retrieve ID of the current queried object. Wrapper for $wp_query->get_queried_object_id()
*
- * @uses WP_Query::get_queried_object_id()
- *
* @since 3.1.0
* @access public
*
+ * @global WP_Query $wp_query
+ *
* @return int
*/
function get_queried_object_id() {
/**
* Set query variable.
*
- * @see WP_Query::set()
* @since 2.2.0
- * @uses $wp_query
*
- * @param string $var Query variable key.
- * @param mixed $value
- * @return null
+ * @global WP_Query $wp_query
+ *
+ * @param string $var Query variable key.
+ * @param mixed $value
*/
-function set_query_var($var, $value) {
+function set_query_var( $var, $value ) {
global $wp_query;
-
- return $wp_query->set($var, $value);
+ $wp_query->set( $var, $value );
}
/**
* once. This must not be used within the WordPress Loop.
*
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param string $query
* @return array List of posts
* wp_query object is not destroyed properly before another is set up.
*
* @since 2.3.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
+ * @global WP_Query $wp_the_query
*/
function wp_reset_query() {
$GLOBALS['wp_query'] = $GLOBALS['wp_the_query'];
* the $post global to the current post in the main query.
*
* @since 3.0.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*/
function wp_reset_postdata() {
global $wp_query;
*
* Month, Year, Category, Author, Post Type archive...
*
- * @see WP_Query::is_archive()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for an existing post type archive page?
*
- * @see WP_Query::is_post_type_archive()
* @since 3.1.0
- * @uses $wp_query
*
- * @param mixed $post_types Optional. Post type or array of posts types to check against.
+ * @global WP_Query $wp_query
+ *
+ * @param string|array $post_types Optional. Post type or array of posts types to check against.
* @return bool
*/
function is_post_type_archive( $post_types = '' ) {
/**
* Is the query for an existing attachment page?
*
- * @see WP_Query::is_attachment()
* @since 2.0.0
- * @uses $wp_query
*
- * @param mixed $attachment Attachment ID, title, slug, or array of such.
+ * @global WP_Query $wp_query
+ *
+ * @param int|string|array|object $attachment Attachment ID, title, slug, or array of such.
* @return bool
*/
function is_attachment( $attachment = '' ) {
* If the $author parameter is specified, this function will additionally
* check if the query is for one of the authors specified.
*
- * @see WP_Query::is_author()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $author Optional. User ID, nickname, nicename, or array of User IDs, nicknames, and nicenames
* @return bool
* If the $category parameter is specified, this function will additionally
* check if the query is for one of the categories specified.
*
- * @see WP_Query::is_category()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $category Optional. Category ID, name, slug, or array of Category IDs, names, and slugs.
* @return bool
* If the $tag parameter is specified, this function will additionally
* check if the query is for one of the tags specified.
*
- * @see WP_Query::is_tag()
* @since 2.3.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $tag Optional. Tag ID, name, slug, or array of Tag IDs, names, and slugs.
* @return bool
* this function will additionally check if the query is for one of the terms
* specified.
*
- * @see WP_Query::is_tax()
* @since 2.5.0
- * @uses $wp_query
*
- * @param mixed $taxonomy Optional. Taxonomy slug or slugs.
- * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
+ * @global WP_Query $wp_query
+ *
+ * @param string|array $taxonomy Optional. Taxonomy slug or slugs.
+ * @param int|string|array $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
* @return bool
*/
function is_tax( $taxonomy = '', $term = '' ) {
/**
* Whether the current URL is within the comments popup window.
*
- * @see WP_Query::is_comments_popup()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for an existing date archive?
*
- * @see WP_Query::is_date()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for an existing day archive?
*
- * @see WP_Query::is_day()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for a feed?
*
- * @see WP_Query::is_feed()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param string|array $feeds Optional feed types to check.
* @return bool
/**
* Is the query for a comments feed?
*
- * @see WP_Query::is_comments_feed()
* @since 3.0.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
*
* Otherwise the same as @see is_home()
*
- * @see WP_Query::is_front_page()
* @since 2.5.0
- * @uses is_home()
- * @uses get_option()
+ *
+ * @global WP_Query $wp_query
*
* @return bool True, if front of site.
*/
*
* @see is_front_page()
*
- * @see WP_Query::is_home()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool True if blog view homepage.
*/
/**
* Is the query for an existing month archive?
*
- * @see WP_Query::is_month()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
* @see is_single()
* @see is_singular()
*
- * @see WP_Query::is_page()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $page Page ID, title, slug, or array of such.
* @return bool
/**
* Is the query for paged result and not for the first page?
*
- * @see WP_Query::is_paged()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for a post or page preview?
*
- * @see WP_Query::is_preview()
* @since 2.0.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for the robots file?
*
- * @see WP_Query::is_robots()
* @since 2.1.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for a search?
*
- * @see WP_Query::is_search()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
* @see is_page()
* @see is_singular()
*
- * @see WP_Query::is_single()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $post Post ID, title, slug, or array of such.
* @return bool
* @see is_page()
* @see is_single()
*
- * @see WP_Query::is_singular()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @param mixed $post_types Optional. Post Type or array of Post Types
* @return bool
/**
* Is the query for a specific time?
*
- * @see WP_Query::is_time()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for a trackback endpoint call?
*
- * @see WP_Query::is_trackback()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query for an existing year archive?
*
- * @see WP_Query::is_year()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Is the query a 404 (returns no results)?
*
- * @see WP_Query::is_404()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
*
* @since 3.3.0
*
+ * @global WP_Query $wp_query
+ *
* @return bool
*/
function is_main_query() {
if ( 'pre_get_posts' === current_filter() ) {
$message = sprintf( __( 'In <code>%1$s</code>, use the <code>%2$s</code> method, not the <code>%3$s</code> function. See %4$s.' ),
- 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'http://codex.wordpress.org/Function_Reference/is_main_query' ) );
+ 'pre_get_posts', 'WP_Query::is_main_query()', 'is_main_query()', __( 'https://codex.wordpress.org/Function_Reference/is_main_query' ) );
_doing_it_wrong( __FUNCTION__, $message, '3.7' );
}
/**
* Whether current WordPress query has results to loop over.
*
- * @see WP_Query::have_posts()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
function have_posts() {
global $wp_query;
-
return $wp_query->have_posts();
}
* Whether the caller is in the Loop.
*
* @since 2.0.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool True if caller is within loop, false if loop hasn't started or ended.
*/
function in_the_loop() {
global $wp_query;
-
return $wp_query->in_the_loop;
}
/**
* Rewind the loop posts.
*
- * @see WP_Query::rewind_posts()
* @since 1.5.0
- * @uses $wp_query
*
- * @return null
+ * @global WP_Query $wp_query
*/
function rewind_posts() {
global $wp_query;
-
- return $wp_query->rewind_posts();
+ $wp_query->rewind_posts();
}
/**
* Iterate the post index in the loop.
*
- * @see WP_Query::the_post()
* @since 1.5.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*/
function the_post() {
global $wp_query;
-
$wp_query->the_post();
}
/**
* Whether there are comments to loop over.
*
- * @see WP_Query::have_comments()
* @since 2.2.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return bool
*/
/**
* Iterate comment index in the comment loop.
*
- * @see WP_Query::the_comment()
* @since 2.2.0
- * @uses $wp_query
+ *
+ * @global WP_Query $wp_query
*
* @return object
*/
/**
* The WordPress Query class.
*
- * @link http://codex.wordpress.org/Function_Reference/WP_Query Codex page.
+ * @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page.
*
* @since 1.5.0
*/
*
* @since 3.1.0
* @access private
+ * @var bool|string
*/
private $query_vars_hash = false;
*/
private $stopwords;
+ private $compat_fields = array( 'query_vars_hash', 'query_vars_changed' );
+
+ private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
+
/**
* Resets query flags to false.
*
* Parse a query string and set query type booleans.
*
* @since 1.5.0
+ * @since 4.2.0 Introduced the ability to order by specific clauses of a `$meta_query`, by passing the clause's
+ * array key to `$orderby`.
* @access public
*
* @param string|array $query {
* or array of fields. 'id=>parent' uses 'id' and 'post_parent'.
* Default all fields. Accepts 'ids', 'id=>parent'.
* @type int $hour Hour of the day. Default empty. Accepts numbers 0-23.
- * @type bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
+ * @type int|bool $ignore_sticky_posts Whether to ignore sticky posts or not. Setting this to false
* excludes stickies from 'post__in'. Accepts 1|true, 0|false.
* Default 0|false.
* @type int $m Combination YearMonth. Accepts any four-digit year and month
* @type int $offset The number of posts to offset before retrieval.
* @type string $order Designates ascending or descending order of posts. Default 'DESC'.
* Accepts 'ASC', 'DESC'.
- * @type string $orderby Sort retrieved posts by parameter. One or more options can be
+ * @type string|array $orderby Sort retrieved posts by parameter. One or more options may be
* passed. To use 'meta_value', or 'meta_value_num',
- * 'meta_key=keyname' must be also be defined. Default 'date'.
- * Accepts 'none', 'name', 'author', 'date', 'title', 'modified',
- * 'menu_order', 'parent', 'ID', 'rand', 'comment_count'.
+ * 'meta_key=keyname' must be also be defined. To sort by a
+ * specific `$meta_query` clause, use that clause's array key.
+ * Default 'date'. Accepts 'none', 'name', 'author', 'date',
+ * 'title', 'modified', 'menu_order', 'parent', 'ID', 'rand',
+ * 'comment_count', 'meta_value', 'meta_value_num', and the
+ * array keys of `$meta_query`.
* @type int $p Post ID.
* @type int $page Show the number of posts that would show up on page X of a
* static front page.
if ( '' !== $qv['menu_order'] ) $qv['menu_order'] = absint($qv['menu_order']);
// Fairly insane upper bound for search string lengths.
- if ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 )
+ if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {
$qv['s'] = '';
+ }
// Compat. Map subpost to attachment.
if ( '' != $qv['subpost'] )
$this->is_date = true;
if ( strlen($qv['m']) > 9 ) {
$this->is_time = true;
- } else if ( strlen($qv['m']) > 7 ) {
+ } elseif ( strlen( $qv['m'] ) > 7 ) {
$this->is_day = true;
- } else if ( strlen($qv['m']) > 5 ) {
+ } elseif ( strlen( $qv['m'] ) > 5 ) {
$this->is_month = true;
} else {
$this->is_year = true;
$this->parse_tax_query( $qv );
foreach ( $this->tax_query->queries as $tax_query ) {
- if ( 'NOT IN' != $tax_query['operator'] ) {
+ if ( ! is_array( $tax_query ) ) {
+ continue;
+ }
+
+ if ( isset( $tax_query['operator'] ) && 'NOT IN' != $tax_query['operator'] ) {
switch ( $tax_query['taxonomy'] ) {
case 'category':
$this->is_category = true;
*
* @param array &$q The query variables
*/
- function parse_tax_query( &$q ) {
+ public function parse_tax_query( &$q ) {
if ( ! empty( $q['tax_query'] ) && is_array( $q['tax_query'] ) ) {
$tax_query = $q['tax_query'];
} else {
$term = $q[$t->query_var];
+ if ( is_array( $term ) ) {
+ $term = implode( ',', $term );
+ }
+
if ( strpos($term, '+') !== false ) {
$terms = preg_split( '/[+]+/', $term );
foreach ( $terms as $term ) {
$tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
$q['tag_slug__in'][] = $tag;
}
- } else if ( preg_match('/[+\r\n\t ]+/', $q['tag']) || !empty($q['cat']) ) {
+ } elseif ( preg_match('/[+\r\n\t ]+/', $q['tag'] ) || ! empty( $q['cat'] ) ) {
$tags = preg_split('/[+\r\n\t ]+/', $q['tag']);
foreach ( (array) $tags as $tag ) {
$tag = sanitize_term_field('slug', $tag, 0, 'post_tag', 'db');
*
* @since 3.7.0
*
- * @param array Terms to check.
+ * @param array $terms Terms to check.
* @return array Terms that are not stopwords.
*/
protected function parse_search_terms( $terms ) {
* 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.
*/
* @since 4.0.0
* @access protected
*
- * @global wpdb $wpdb WordPress database access abstraction object.
+ * @global wpdb $wpdb WordPress database 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.
+ * @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
'parent', 'type', 'ID', 'menu_order', 'comment_count', 'rand',
);
- $meta_key = $this->get( 'meta_key' );
- if ( ! empty( $meta_key ) ) {
- $allowed_keys[] = $meta_key;
+ $primary_meta_key = '';
+ $primary_meta_query = false;
+ $meta_clauses = $this->meta_query->get_clauses();
+ if ( ! empty( $meta_clauses ) ) {
+ $primary_meta_query = reset( $meta_clauses );
+
+ if ( ! empty( $primary_meta_query['key'] ) ) {
+ $primary_meta_key = $primary_meta_query['key'];
+ $allowed_keys[] = $primary_meta_key;
+ }
+
$allowed_keys[] = 'meta_value';
$allowed_keys[] = 'meta_value_num';
+ $allowed_keys = array_merge( $allowed_keys, array_keys( $meta_clauses ) );
}
if ( ! in_array( $orderby, $allowed_keys ) ) {
case 'ID':
case 'menu_order':
case 'comment_count':
- $orderby = "$wpdb->posts.{$orderby}";
+ $orderby_clause = "$wpdb->posts.{$orderby}";
break;
case 'rand':
- $orderby = 'RAND()';
+ $orderby_clause = 'RAND()';
break;
- case $meta_key:
+ case $primary_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})";
+ if ( ! empty( $primary_meta_query['type'] ) ) {
+ $orderby_clause = "CAST({$primary_meta_query['alias']}.meta_value AS {$primary_meta_query['cast']})";
} else {
- $orderby = "$wpdb->postmeta.meta_value";
+ $orderby_clause = "{$primary_meta_query['alias']}.meta_value";
}
break;
case 'meta_value_num':
- $orderby = "$wpdb->postmeta.meta_value+0";
+ $orderby_clause = "{$primary_meta_query['alias']}.meta_value+0";
break;
default:
- $orderby = "$wpdb->posts.post_" . $orderby;
+ if ( array_key_exists( $orderby, $meta_clauses ) ) {
+ // $orderby corresponds to a meta_query clause.
+ $meta_clause = $meta_clauses[ $orderby ];
+ $orderby_clause = "CAST({$meta_clause['alias']}.meta_value AS {$meta_clause['cast']})";
+ } else {
+ // Default: order by post field.
+ $orderby_clause = "$wpdb->posts.post_" . sanitize_key( $orderby );
+ }
+
break;
}
- return $orderby;
+ return $orderby_clause;
}
/**
* @access public
*
* @param string $query_var Query variable key.
- * @param mixed $value Query variable value.
+ * @param mixed $value Query variable value.
*/
public function set($query_var, $value) {
$this->query_vars[$query_var] = $value;
*
* @since 1.5.0
* @access public
- * @uses do_action_ref_array() Calls 'pre_get_posts' hook before retrieving posts.
+ *
+ * @global wpdb $wpdb
*
* @return array List of posts.
*/
$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 )
+ elseif ( $q['posts_per_page'] == 0 )
$q['posts_per_page'] = 1;
if ( !isset($q['comments_per_page']) || $q['comments_per_page'] == 0 )
if ( empty($post_type) ) {
// Do a fully inclusive search for currently registered post types of queried taxonomies
$post_type = array();
- $taxonomies = wp_list_pluck( $this->tax_query->queries, 'taxonomy' );
+ $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 ) )
}
}
- // Back-compat
- if ( !empty($this->tax_query->queries) ) {
- $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
- if ( !empty( $tax_query_in_and ) ) {
- if ( !isset( $q['taxonomy'] ) ) {
- foreach ( $tax_query_in_and as $a_tax_query ) {
- if ( !in_array( $a_tax_query['taxonomy'], array( 'category', 'post_tag' ) ) ) {
- $q['taxonomy'] = $a_tax_query['taxonomy'];
- if ( 'slug' == $a_tax_query['field'] )
- $q['term'] = $a_tax_query['terms'][0];
- else
- $q['term_id'] = $a_tax_query['terms'][0];
+ /*
+ * Ensure that 'taxonomy', 'term', 'term_id', 'cat', and
+ * 'category_name' vars are set for backward compatibility.
+ */
+ if ( ! empty( $this->tax_query->queried_terms ) ) {
- break;
- }
+ /*
+ * 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;
}
- }
- $cat_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'category' ) );
- if ( ! empty( $cat_query ) ) {
- $cat_query = reset( $cat_query );
+ if ( ! in_array( $queried_taxonomy, array( 'category', 'post_tag' ) ) ) {
+ $q['taxonomy'] = $queried_taxonomy;
- if ( ! empty( $cat_query['terms'][0] ) ) {
- $the_cat = get_term_by( $cat_query['field'], $cat_query['terms'][0], 'category' );
- if ( $the_cat ) {
- $this->set( 'cat', $the_cat->term_id );
- $this->set( 'category_name', $the_cat->slug );
+ if ( 'slug' === $queried_items['field'] ) {
+ $q['term'] = $queried_items['terms'][0];
+ } else {
+ $q['term_id'] = $queried_items['terms'][0];
}
- unset( $the_cat );
}
}
- unset( $cat_query );
+ }
- $tag_query = wp_list_filter( $tax_query_in_and, array( 'taxonomy' => 'post_tag' ) );
- if ( ! empty( $tag_query ) ) {
- $tag_query = reset( $tag_query );
+ // 'cat', 'category_name', 'tag_id'
+ foreach ( $this->tax_query->queried_terms as $queried_taxonomy => $queried_items ) {
+ if ( empty( $queried_items['terms'][0] ) ) {
+ continue;
+ }
- if ( ! empty( $tag_query['terms'][0] ) ) {
- $the_tag = get_term_by( $tag_query['field'], $tag_query['terms'][0], 'post_tag' );
- if ( $the_tag )
- $this->set( 'tag_id', $the_tag->term_id );
- unset( $the_tag );
+ 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 );
}
- unset( $tag_query );
}
}
$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'] = 'DESC';
+ $q['order'] = $rand ? '' : 'DESC';
} else {
- $q['order'] = $this->parse_order( $q['order'] );
+ $q['order'] = $rand ? '' : $this->parse_order( $q['order'] );
}
// Order by.
$orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
if ( empty( $orderby ) ) {
- $orderby = "$wpdb->posts.post_date ".$q['order'];
- } else {
+ $orderby = "$wpdb->posts.post_date " . $q['order'];
+ } elseif ( ! empty( $q['order'] ) ) {
$orderby .= " {$q['order']}";
}
}
$user_id = get_current_user_id();
+ $q_status = array();
if ( ! empty( $q['post_status'] ) ) {
$statuswheres = array();
$q_status = $q['post_status'];
$where .= ')';
}
- if ( !empty( $this->meta_query->queries ) ) {
- $clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
- $join .= $clauses['join'];
- $where .= $clauses['where'];
- }
-
/*
* Apply filters on where and join prior to paging so that any
* manipulations to them are reflected in the paging by day queries.
if ( 'ids' == $q['fields'] ) {
$this->posts = $wpdb->get_col( $this->request );
+ $this->posts = array_map( 'intval', $this->posts );
$this->post_count = count( $this->posts );
$this->set_found_posts( $q, $limits );
- return array_map( 'intval', $this->posts );
+ return $this->posts;
}
if ( 'id=>parent' == $q['fields'] ) {
$this->set_found_posts( $q, $limits );
$r = array();
- foreach ( $this->posts as $post ) {
+ foreach ( $this->posts as $key => $post ) {
+ $this->posts[ $key ]->ID = (int) $post->ID;
+ $this->posts[ $key ]->post_parent = (int) $post->post_parent;
+
$r[ (int) $post->ID ] = (int) $post->post_parent;
}
+
return $r;
}
$status = get_post_status($this->posts[0]);
$post_status_obj = get_post_status_object($status);
//$type = get_post_type($this->posts[0]);
- if ( !$post_status_obj->public ) {
+
+ // If the post_status was specifically requested, let it pass through.
+ if ( !$post_status_obj->public && ! in_array( $status, $q_status ) ) {
+
if ( ! is_user_logged_in() ) {
// User must be logged in to view unpublished posts.
$this->posts = array();
*
* @since 3.5.0
* @access private
+ *
+ * @global wpdb $wpdb
*/
private function set_found_posts( $q, $limits ) {
global $wpdb;
*
* @since 1.5.0
* @access public
- * @uses $post
- * @uses do_action_ref_array() Calls 'loop_start' if loop has just started
+ *
+ * @global WP_Post $post
*/
public function the_post() {
global $post;
do_action_ref_array( 'loop_start', array( &$this ) );
$post = $this->next_post();
- setup_postdata($post);
+ $this->setup_postdata( $post );
}
/**
*
* @since 1.5.0
* @access public
- * @uses do_action_ref_array() Calls 'loop_end' if loop is ended
*
* @return bool True if posts are available, false if end of loop.
*/
* @since 2.2.0
* @access public
* @global object $comment Current comment.
- * @uses do_action() Calls 'comment_loop_start' hook when first comment is processed.
*/
public function the_comment() {
global $comment;
$term = get_term_by( 'slug', $this->get( 'tag' ), 'post_tag' );
}
} else {
- $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
- $query = reset( $tax_query_in_and );
+ // For other tax queries, grab the first term from the first clause.
+ $tax_query_in_and = wp_list_filter( $this->tax_query->queried_terms, array( 'operator' => 'NOT IN' ), 'NOT' );
- if ( $query['terms'] ) {
- if ( 'term_id' == $query['field'] ) {
- $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
- } else {
- $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
+ if ( ! empty( $tax_query_in_and ) ) {
+ $queried_taxonomies = array_keys( $tax_query_in_and );
+ $matched_taxonomy = reset( $queried_taxonomies );
+ $query = $tax_query_in_and[ $matched_taxonomy ];
+
+ if ( $query['terms'] ) {
+ if ( 'term_id' == $query['field'] ) {
+ $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
+ } else {
+ $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
+ }
}
}
}
* @since 1.5.0
* @access public
*
- * @param string $query URL query string.
- * @return WP_Query
+ * @param string|array $query URL query string or array of vars.
*/
public function __construct($query = '') {
if ( ! empty($query) ) {
* @return mixed Property.
*/
public function __get( $name ) {
- return $this->$name;
+ if ( in_array( $name, $this->compat_fields ) ) {
+ return $this->$name;
+ }
}
/**
- * Make private properties settable for backwards compatibility.
+ * Make private properties checkable for backwards compatibility.
*
* @since 4.0.0
* @access public
* @return bool Whether the property is set.
*/
public function __isset( $name ) {
- return isset( $this->$name );
- }
-
- /**
- * Make private properties settable for backwards compatibility.
- *
- * @since 4.0.0
- * @access public
- *
- * @param string $name Property to unset.
- */
- public function __unset( $name ) {
- unset( $this->$name );
+ if ( in_array( $name, $this->compat_fields ) ) {
+ return isset( $this->$name );
+ }
}
/**
*
* @param callable $name Method to call.
* @param array $arguments Arguments to pass when calling.
- * @return mixed|bool Return value of the callback, otherwise false.
+ * @return mixed|false Return value of the callback, false otherwise.
*/
public function __call( $name, $arguments ) {
- return call_user_func_array( array( $this, $name ), $arguments );
+ if ( in_array( $name, $this->compat_methods ) ) {
+ return call_user_func_array( array( $this, $name ), $arguments );
+ }
+ return false;
}
/**
$post_obj = $this->get_queried_object();
- if ( in_array( $post_obj->ID, $attachment ) ) {
+ if ( in_array( (string) $post_obj->ID, $attachment ) ) {
return true;
} elseif ( in_array( $post_obj->post_title, $attachment ) ) {
return true;
$author = (array) $author;
- if ( in_array( $author_obj->ID, $author ) )
+ if ( in_array( (string) $author_obj->ID, $author ) )
return true;
elseif ( in_array( $author_obj->nickname, $author ) )
return true;
$category = (array) $category;
- if ( in_array( $cat_obj->term_id, $category ) )
+ if ( in_array( (string) $cat_obj->term_id, $category ) )
return true;
elseif ( in_array( $cat_obj->name, $category ) )
return true;
$tag = (array) $tag;
- if ( in_array( $tag_obj->term_id, $tag ) )
+ if ( in_array( (string) $tag_obj->term_id, $tag ) )
return true;
elseif ( in_array( $tag_obj->name, $tag ) )
return true;
*
* @since 3.1.0
*
+ * @global array $wp_taxonomies
+ *
* @param mixed $taxonomy Optional. Taxonomy slug or slugs.
- * @param mixed $term. Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
+ * @param mixed $term Optional. Term ID, name, slug or array of Term IDs, names, and slugs.
* @return bool
*/
public function is_tax( $taxonomy = '', $term = '' ) {
* Otherwise the same as @see WP_Query::is_home()
*
* @since 3.1.0
- * @uses is_home()
- * @uses get_option()
*
* @return bool True, if front of site.
*/
$page = (array) $page;
- if ( in_array( $page_obj->ID, $page ) ) {
+ if ( in_array( (string) $page_obj->ID, $page ) ) {
return true;
} elseif ( in_array( $page_obj->post_title, $page ) ) {
return true;
- } else if ( in_array( $page_obj->post_name, $page ) ) {
+ } elseif ( in_array( $page_obj->post_name, $page ) ) {
return true;
} else {
foreach ( $page as $pagepath ) {
$post = (array) $post;
- if ( in_array( $post_obj->ID, $post ) ) {
+ if ( in_array( (string) $post_obj->ID, $post ) ) {
return true;
} elseif ( in_array( $post_obj->post_title, $post ) ) {
return true;
*
* @since 3.3.0
*
+ * @global WP_Query $wp_query
+ *
* @return bool
*/
public function is_main_query() {
return $wp_the_query === $this;
}
+ /**
+ * Set up global post data.
+ *
+ * @since 4.1.0
+ *
+ * @global int $id
+ * @global WP_User $authordata
+ * @global string|int|bool $currentday
+ * @global string|int|bool $currentmonth
+ * @global int $page
+ * @global array $pages
+ * @global int $multipage
+ * @global int $more
+ * @global int $numpages
+ *
+ * @param WP_Post $post Post data.
+ * @return true True when finished.
+ */
+ public function setup_postdata( $post ) {
+ global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
+
+ $id = (int) $post->ID;
+
+ $authordata = get_userdata($post->post_author);
+
+ $currentday = mysql2date('d.m.y', $post->post_date, false);
+ $currentmonth = mysql2date('m', $post->post_date, false);
+ $numpages = 1;
+ $multipage = 0;
+ $page = $this->get( 'page' );
+ if ( ! $page )
+ $page = 1;
+
+ /*
+ * Force full post content when viewing the permalink for the $post,
+ * or when on an RSS feed. Otherwise respect the 'more' tag.
+ */
+ if ( $post->ID === get_queried_object_id() && ( $this->is_page() || $this->is_single() ) ) {
+ $more = 1;
+ } elseif ( $this->is_feed() ) {
+ $more = 1;
+ } else {
+ $more = 0;
+ }
+
+ $content = $post->post_content;
+ if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
+ if ( $page > 1 )
+ $more = 1;
+ $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
+ $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
+ $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
+
+ // Ignore nextpage at the beginning of the content.
+ if ( 0 === strpos( $content, '<!--nextpage-->' ) )
+ $content = substr( $content, 15 );
+
+ $pages = explode('<!--nextpage-->', $content);
+ $numpages = count($pages);
+ if ( $numpages > 1 )
+ $multipage = 1;
+ } else {
+ $pages = array( $post->post_content );
+ }
+
+ /**
+ * Fires once the post data has been setup.
+ *
+ * @since 2.8.0
+ * @since 4.1.0 Introduced `$this` parameter.
+ *
+ * @param WP_Post &$post The Post object (passed by reference).
+ * @param WP_Query &$this The current Query object (passed by reference).
+ */
+ do_action_ref_array( 'the_post', array( &$post, &$this ) );
+
+ return true;
+ }
/**
* After looping through a nested query, this function
* restores the $post global to the current post in this query.
*
* @since 3.7.0
*
- * @return bool
+ * @global WP_Post $post
*/
public function reset_postdata() {
if ( ! empty( $this->post ) ) {
* Attempts to find the current slug from the past slugs.
*
* @since 2.1.0
- * @uses $wp_query
- * @uses $wpdb
*
- * @return null If no link is found, null is returned.
+ * @global WP_Query $wp_query
+ * @global wpdb $wpdb WordPress database abstraction object.
*/
function wp_old_slug_redirect() {
global $wp_query;
if ( is_array( $post_type ) ) {
if ( count( $post_type ) > 1 )
return;
- $post_type = array_shift( $post_type );
+ $post_type = reset( $post_type );
}
// Do not attempt redirect for hierarchical post types
*
* @since 1.5.0
*
+ * @global WP_Query $wp_query
+ *
* @param object $post Post data.
- * @uses do_action_ref_array() Calls 'the_post'
* @return bool True when finished.
*/
function setup_postdata( $post ) {
- global $id, $authordata, $currentday, $currentmonth, $page, $pages, $multipage, $more, $numpages;
-
- $id = (int) $post->ID;
-
- $authordata = get_userdata($post->post_author);
+ global $wp_query;
- $currentday = mysql2date('d.m.y', $post->post_date, false);
- $currentmonth = mysql2date('m', $post->post_date, false);
- $numpages = 1;
- $multipage = 0;
- $page = get_query_var('page');
- if ( ! $page )
- $page = 1;
- if ( is_single() || is_page() || is_feed() )
- $more = 1;
- $content = $post->post_content;
- if ( false !== strpos( $content, '<!--nextpage-->' ) ) {
- if ( $page > 1 )
- $more = 1;
- $content = str_replace( "\n<!--nextpage-->\n", '<!--nextpage-->', $content );
- $content = str_replace( "\n<!--nextpage-->", '<!--nextpage-->', $content );
- $content = str_replace( "<!--nextpage-->\n", '<!--nextpage-->', $content );
- // Ignore nextpage at the beginning of the content.
- if ( 0 === strpos( $content, '<!--nextpage-->' ) )
- $content = substr( $content, 15 );
- $pages = explode('<!--nextpage-->', $content);
- $numpages = count($pages);
- if ( $numpages > 1 )
- $multipage = 1;
- } else {
- $pages = array( $post->post_content );
+ if ( ! empty( $wp_query ) && $wp_query instanceof WP_Query ) {
+ return $wp_query->setup_postdata( $post );
}
- /**
- * Fires once the post data has been setup.
- *
- * @since 2.8.0
- *
- * @param WP_Post &$post The Post object (passed by reference).
- */
- do_action_ref_array( 'the_post', array( &$post ) );
-
- return true;
+ return false;
}