WP_Query::parse_query() – Method | Developer.WordPress.org
Parses a query string and sets query type booleans.
Parameters
$querystring|arrayoptionalArray or string of Query parameters.
attachment_idintAttachment post ID. Used for
'attachment'post_type.authorint|stringAuthor ID, or comma-separated list of IDs.
author_namestringUser
'user_nicename'.author__inint[]An array of author IDs to query from.
author__not_inint[]An array of author IDs not to query from.
cache_resultsboolWhether to cache post information. Default true.
catint|stringCategory ID or comma-separated list of IDs (this or any children).
category__andint[]An array of category IDs (AND in).
category__inint[]An array of category IDs (OR in, no children).
category__not_inint[]An array of category IDs (NOT in).
category_namestringUse category slug (not name, this or any children).
comment_countarray|intFilter results by comment count. Provide an integer to match comment count exactly. Provide an array with integer
'value'and'compare'operator ('=','!=','>','>=','<','<=') to compare against comment_count in a specific way.comment_statusstringComment status.
comments_per_pageintThe number of comments to return per page.
Default'comments_per_page'option.date_queryarraydayintDay of the month. Default empty. Accepts numbers 1-31.
exactboolWhether to search by exact keyword. Default false.
fieldsstringPost fields to query for. Accepts:
''Returns an array of complete post objects (WP_Post[]).'ids'Returns an array of post IDs (int[]).'id=>parent'Returns an associative array of parent post IDs, keyed by post ID (int[]).
Default
''.hourintHour of the day. Default empty. Accepts numbers 0-23.
ignore_sticky_postsint|boolWhether to ignore sticky posts or not. Setting this to false excludes stickies from
'post__in'. Accepts1|true,0|false.
Default false.mintCombination YearMonth. Accepts any four-digit year and month numbers 01-12. Default empty.
meta_keystring|string[]Meta key or keys to filter by.
meta_valuestring|string[]Meta value or values to filter by.
meta_comparestringMySQL operator used for comparing the meta value.
See WP_Meta_Query::__construct() for accepted values and default value.meta_compare_keystringMySQL operator used for comparing the meta key.
See WP_Meta_Query::__construct() for accepted values and default value.meta_typestringMySQL data type that the meta_value column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_type_keystringMySQL data type that the meta_key column will be CAST to for comparisons.
See WP_Meta_Query::__construct() for accepted values and default value.meta_queryarraymenu_orderintThe menu order of the posts.
minuteintMinute of the hour. Default empty. Accepts numbers 0-59.
monthnumintThe two-digit month. Default empty. Accepts numbers 1-12.
namestringPost slug.
nopagingboolShow all posts (true) or paginate (false). Default false.
no_found_rowsboolWhether to skip counting the total rows found. Enabling can improve performance. Default false.
offsetintThe number of posts to offset before retrieval.
orderstringDesignates ascending or descending order of posts. Default
'DESC'.
Accepts'ASC','DESC'.orderbystring|arraySort 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. To sort by a specific$meta_queryclause, use that clause’s array key. Accepts:'none''name''author''date''title''modified''menu_order''parent''ID''rand''relevance''RAND(x)'(where'x'is an integer seed value)'comment_count''meta_value''meta_value_num''post__in''post_name__in''post_parent__in'- The array keys of
$meta_query.
Default is
'date', except when a search is being performed, when the default is'relevance'.pintPost ID.
pageintShow the number of posts that would show up on page X of a static front page.
pagedintThe number of the current page.
page_idintPage ID.
pagenamestringPage slug.
permstringShow posts if user has the appropriate capability.
ping_statusstringPing status.
post__inint[]An array of post IDs to retrieve, sticky posts will be included.
post__not_inint[]An array of post IDs not to retrieve. Note: a string of comma- separated IDs will NOT work.
post_mime_typestringThe mime type of the post. Used for
'attachment'post_type.post_name__instring[]An array of post slugs that results must match.
post_parentintPage ID to retrieve child pages for. Use 0 to only retrieve top-level pages.
post_parent__inint[]An array containing parent page IDs to query child pages from.
post_parent__not_inint[]An array containing parent page IDs not to query child pages from.
post_typestring|string[]A post type slug (string) or array of post type slugs.
Default'any'if using'tax_query'.post_statusstring|string[]A post status (string) or array of post statuses.
posts_per_pageintThe number of posts to query for. Use -1 to request all posts.
posts_per_archive_pageintThe number of posts to query for by archive page. Overrides
'posts_per_page'when is_archive() , or is_search() are true.sstringSearch keyword(s). Prepending a term with a hyphen will exclude posts matching that term. Eg, ‘pillow -sofa’ will return posts containing
'pillow'but not'sofa'. The character used for exclusion can be modified using the the'wp_query_search_exclusion_prefix'filter.search_columnsstring[]Array of column names to be searched. Accepts
'post_title','post_excerpt'and'post_content'. Default empty array.secondintSecond of the minute. Default empty. Accepts numbers 0-59.
sentenceboolWhether to search by phrase. Default false.
suppress_filtersboolWhether to suppress filters. Default false.
tagstringTag slug. Comma-separated (either), Plus-separated (all).
tag__andint[]An array of tag IDs (AND in).
tag__inint[]An array of tag IDs (OR in).
tag__not_inint[]An array of tag IDs (NOT in).
tag_idintTag id or comma-separated list of IDs.
tag_slug__andstring[]An array of tag slugs (AND in).
tag_slug__instring[]An array of tag slugs (OR in). unless
'ignore_sticky_posts'is true. Note: a string of comma-separated IDs will NOT work.tax_queryarraytitlestringPost title.
update_post_meta_cacheboolWhether to update the post meta cache. Default true.
update_post_term_cacheboolWhether to update the post term cache. Default true.
update_menu_item_cacheboolWhether to update the menu item cache. Default false.
lazy_load_term_metaboolWhether to lazy-load term meta. Setting to false will disable cache priming for term meta, so that each get_term_meta() call will hit the database.
Defaults to the value of$update_post_term_cache.wintThe week number of the year. Default empty. Accepts numbers 0-53.
yearintThe four-digit year. Default empty. Accepts any four-digit year.
Source
public function parse_query( $query = '' ) {
if ( ! empty( $query ) ) {
$this->init();
$this->query = wp_parse_args( $query );
$this->query_vars = $this->query;
} elseif ( ! isset( $this->query ) ) {
$this->query = $this->query_vars;
}
$this->query_vars = $this->fill_query_vars( $this->query_vars );
$query_vars = &$this->query_vars;
$this->query_vars_changed = true;
if ( ! empty( $query_vars['robots'] ) ) {
$this->is_robots = true;
} elseif ( ! empty( $query_vars['favicon'] ) ) {
$this->is_favicon = true;
}
if ( ! is_scalar( $query_vars['p'] ) || (int) $query_vars['p'] < 0 ) {
$query_vars['p'] = 0;
$query_vars['error'] = '404';
} else {
$query_vars['p'] = (int) $query_vars['p'];
}
$query_vars['page_id'] = is_scalar( $query_vars['page_id'] ) ? absint( $query_vars['page_id'] ) : 0;
$query_vars['year'] = is_scalar( $query_vars['year'] ) ? absint( $query_vars['year'] ) : 0;
$query_vars['monthnum'] = is_scalar( $query_vars['monthnum'] ) ? absint( $query_vars['monthnum'] ) : 0;
$query_vars['day'] = is_scalar( $query_vars['day'] ) ? absint( $query_vars['day'] ) : 0;
$query_vars['w'] = is_scalar( $query_vars['w'] ) ? absint( $query_vars['w'] ) : 0;
$query_vars['m'] = is_scalar( $query_vars['m'] ) ? preg_replace( '|[^0-9]|', '', $query_vars['m'] ) : '';
$query_vars['paged'] = is_scalar( $query_vars['paged'] ) ? absint( $query_vars['paged'] ) : 0;
$query_vars['cat'] = preg_replace( '|[^0-9,-]|', '', $query_vars['cat'] ); // Array or comma-separated list of positive or negative integers.
$query_vars['author'] = is_scalar( $query_vars['author'] ) ? preg_replace( '|[^0-9,-]|', '', $query_vars['author'] ) : ''; // Comma-separated list of positive or negative integers.
$query_vars['pagename'] = is_scalar( $query_vars['pagename'] ) ? trim( $query_vars['pagename'] ) : '';
$query_vars['name'] = is_scalar( $query_vars['name'] ) ? trim( $query_vars['name'] ) : '';
$query_vars['title'] = is_scalar( $query_vars['title'] ) ? trim( $query_vars['title'] ) : '';
if ( is_scalar( $query_vars['hour'] ) && '' !== $query_vars['hour'] ) {
$query_vars['hour'] = absint( $query_vars['hour'] );
} else {
$query_vars['hour'] = '';
}
if ( is_scalar( $query_vars['minute'] ) && '' !== $query_vars['minute'] ) {
$query_vars['minute'] = absint( $query_vars['minute'] );
} else {
$query_vars['minute'] = '';
}
if ( is_scalar( $query_vars['second'] ) && '' !== $query_vars['second'] ) {
$query_vars['second'] = absint( $query_vars['second'] );
} else {
$query_vars['second'] = '';
}
if ( is_scalar( $query_vars['menu_order'] ) && '' !== $query_vars['menu_order'] ) {
$query_vars['menu_order'] = absint( $query_vars['menu_order'] );
} else {
$query_vars['menu_order'] = '';
}
// Fairly large, potentially too large, upper bound for search string lengths.
if ( ! is_scalar( $query_vars['s'] ) || ( ! empty( $query_vars['s'] ) && strlen( $query_vars['s'] ) > 1600 ) ) {
$query_vars['s'] = '';
}
// Compat. Map subpost to attachment.
if ( is_scalar( $query_vars['subpost'] ) && '' != $query_vars['subpost'] ) {
$query_vars['attachment'] = $query_vars['subpost'];
}
if ( is_scalar( $query_vars['subpost_id'] ) && '' != $query_vars['subpost_id'] ) {
$query_vars['attachment_id'] = $query_vars['subpost_id'];
}
$query_vars['attachment_id'] = is_scalar( $query_vars['attachment_id'] ) ? absint( $query_vars['attachment_id'] ) : 0;
if ( ( '' !== $query_vars['attachment'] ) || ! empty( $query_vars['attachment_id'] ) ) {
$this->is_single = true;
$this->is_attachment = true;
} elseif ( '' !== $query_vars['name'] ) {
$this->is_single = true;
} elseif ( $query_vars['p'] ) {
$this->is_single = true;
} elseif ( '' !== $query_vars['pagename'] || ! empty( $query_vars['page_id'] ) ) {
$this->is_page = true;
$this->is_single = false;
} else {
// Look for archive queries. Dates, categories, authors, search, post type archives.
if ( isset( $this->query['s'] ) ) {
$this->is_search = true;
}
if ( '' !== $query_vars['second'] ) {
$this->is_time = true;
$this->is_date = true;
}
if ( '' !== $query_vars['minute'] ) {
$this->is_time = true;
$this->is_date = true;
}
if ( '' !== $query_vars['hour'] ) {
$this->is_time = true;
$this->is_date = true;
}
if ( $query_vars['day'] ) {
if ( ! $this->is_date ) {
$date = sprintf( '%04d-%02d-%02d', $query_vars['year'], $query_vars['monthnum'], $query_vars['day'] );
if ( $query_vars['monthnum'] && $query_vars['year'] && ! wp_checkdate( $query_vars['monthnum'], $query_vars['day'], $query_vars['year'], $date ) ) {
$query_vars['error'] = '404';
} else {
$this->is_day = true;
$this->is_date = true;
}
}
}
if ( $query_vars['monthnum'] ) {
if ( ! $this->is_date ) {
if ( 12 < $query_vars['monthnum'] ) {
$query_vars['error'] = '404';
} else {
$this->is_month = true;
$this->is_date = true;
}
}
}
if ( $query_vars['year'] ) {
if ( ! $this->is_date ) {
$this->is_year = true;
$this->is_date = true;
}
}
if ( $query_vars['m'] ) {
$this->is_date = true;
if ( strlen( $query_vars['m'] ) > 9 ) {
$this->is_time = true;
} elseif ( strlen( $query_vars['m'] ) > 7 ) {
$this->is_day = true;
} elseif ( strlen( $query_vars['m'] ) > 5 ) {
$this->is_month = true;
} else {
$this->is_year = true;
}
}
if ( $query_vars['w'] ) {
$this->is_date = true;
}
$this->query_vars_hash = false;
$this->parse_tax_query( $query_vars );
foreach ( $this->tax_query->queries as $tax_query ) {
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;
break;
case 'post_tag':
$this->is_tag = true;
break;
default:
$this->is_tax = true;
}
}
}
unset( $tax_query );
if ( empty( $query_vars['author'] ) || ( '0' == $query_vars['author'] ) ) {
$this->is_author = false;
} else {
$this->is_author = true;
}
if ( '' !== $query_vars['author_name'] ) {
$this->is_author = true;
}
if ( ! empty( $query_vars['post_type'] ) && ! is_array( $query_vars['post_type'] ) ) {
$post_type_obj = get_post_type_object( $query_vars['post_type'] );
if ( ! empty( $post_type_obj->has_archive ) ) {
$this->is_post_type_archive = true;
}
}
if ( $this->is_post_type_archive || $this->is_date || $this->is_author || $this->is_category || $this->is_tag || $this->is_tax ) {
$this->is_archive = true;
}
}
if ( '' != $query_vars['feed'] ) {
$this->is_feed = true;
}
if ( '' != $query_vars['embed'] ) {
$this->is_embed = true;
}
if ( '' != $query_vars['tb'] ) {
$this->is_trackback = true;
}
if ( '' != $query_vars['paged'] && ( (int) $query_vars['paged'] > 1 ) ) {
$this->is_paged = true;
}
// If we're previewing inside the write screen.
if ( '' != $query_vars['preview'] ) {
$this->is_preview = true;
}
if ( is_admin() ) {
$this->is_admin = true;
}
if ( str_contains( $query_vars['feed'], 'comments-' ) ) {
$query_vars['feed'] = str_replace( 'comments-', '', $query_vars['feed'] );
$query_vars['withcomments'] = 1;
}
$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
if ( $this->is_feed && ( ! empty( $query_vars['withcomments'] ) || ( empty( $query_vars['withoutcomments'] ) && $this->is_singular ) ) ) {
$this->is_comment_feed = true;
}
if ( ! ( $this->is_singular || $this->is_archive || $this->is_search || $this->is_feed
|| ( wp_is_serving_rest_request() && $this->is_main_query() )
|| $this->is_trackback || $this->is_404 || $this->is_admin || $this->is_robots || $this->is_favicon ) ) {
$this->is_home = true;
}
// Correct `is_*` for 'page_on_front' and 'page_for_posts'.
if ( $this->is_home && 'page' === get_option( 'show_on_front' ) && get_option( 'page_on_front' ) ) {
$_query = wp_parse_args( $this->query );
// 'pagename' can be set and empty depending on matched rewrite rules. Ignore an empty 'pagename'.
if ( isset( $_query['pagename'] ) && '' === $_query['pagename'] ) {
unset( $_query['pagename'] );
}
unset( $_query['embed'] );
if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage' ) ) ) {
$this->is_page = true;
$this->is_home = false;
$query_vars['page_id'] = get_option( 'page_on_front' );
// Correct <!--nextpage--> for 'page_on_front'.
if ( ! empty( $query_vars['paged'] ) ) {
$query_vars['page'] = $query_vars['paged'];
unset( $query_vars['paged'] );
}
}
}
if ( '' !== $query_vars['pagename'] ) {
$this->queried_object = get_page_by_path( $query_vars['pagename'] );
if ( $this->queried_object && 'attachment' === $this->queried_object->post_type ) {
if ( preg_match( '/^[^%]*%(?:postname)%/', get_option( 'permalink_structure' ) ) ) {
// See if we also have a post with the same slug.
$post = get_page_by_path( $query_vars['pagename'], OBJECT, 'post' );
if ( $post ) {
$this->queried_object = $post;
$this->is_page = false;
$this->is_single = true;
}
}
}
if ( ! empty( $this->queried_object ) ) {
$this->queried_object_id = (int) $this->queried_object->ID;
} else {
unset( $this->queried_object );
}
if ( 'page' === get_option( 'show_on_front' ) && isset( $this->queried_object_id ) && get_option( 'page_for_posts' ) == $this->queried_object_id ) {
$this->is_page = false;
$this->is_home = true;
$this->is_posts_page = true;
}
if ( isset( $this->queried_object_id ) && get_option( 'wp_page_for_privacy_policy' ) == $this->queried_object_id ) {
$this->is_privacy_policy = true;
}
}
if ( $query_vars['page_id'] ) {
if ( 'page' === get_option( 'show_on_front' ) && get_option( 'page_for_posts' ) == $query_vars['page_id'] ) {
$this->is_page = false;
$this->is_home = true;
$this->is_posts_page = true;
}
if ( get_option( 'wp_page_for_privacy_policy' ) == $query_vars['page_id'] ) {
$this->is_privacy_policy = true;
}
}
if ( ! empty( $query_vars['post_type'] ) ) {
if ( is_array( $query_vars['post_type'] ) ) {
$query_vars['post_type'] = array_map( 'sanitize_key', array_unique( $query_vars['post_type'] ) );
sort( $query_vars['post_type'] );
} else {
$query_vars['post_type'] = sanitize_key( $query_vars['post_type'] );
}
}
if ( ! empty( $query_vars['post_status'] ) ) {
if ( is_array( $query_vars['post_status'] ) ) {
$query_vars['post_status'] = array_map( 'sanitize_key', array_unique( $query_vars['post_status'] ) );
sort( $query_vars['post_status'] );
} else {
$query_vars['post_status'] = preg_replace( '|[^a-z0-9_,-]|', '', $query_vars['post_status'] );
}
}
if ( $this->is_posts_page && ( ! isset( $query_vars['withcomments'] ) || ! $query_vars['withcomments'] ) ) {
$this->is_comment_feed = false;
}
$this->is_singular = $this->is_single || $this->is_page || $this->is_attachment;
// Done correcting `is_*` for 'page_on_front' and 'page_for_posts'.
if ( '404' == $query_vars['error'] ) {
$this->set_404();
}
$this->is_embed = $this->is_embed && ( $this->is_singular || $this->is_404 );
$this->query_vars_hash = md5( serialize( $this->query_vars ) );
$this->query_vars_changed = false;
/**
* Fires after the main query vars have been parsed.
*
* @since 1.5.0
*
* @param WP_Query $query The WP_Query instance (passed by reference).
*/
do_action_ref_array( 'parse_query', array( &$this ) );
}
Hooks
- do_action_ref_array( ‘parse_query’,
WP_Query $query ) Fires after the main query vars have been parsed.
Changelog
| Version | Description |
|---|---|
| 6.2.0 | Introduced the $search_columns parameter. |
| 6.1.0 | Introduced the $update_menu_item_cache parameter. |
| 5.3.0 | Introduced the $meta_type_key parameter. |
| 5.1.0 | Introduced the $meta_compare_key parameter. |
| 4.9.0 | Introduced the $comment_count parameter. |
| 4.6.0 | Added 'post_name__in' support for $orderby. Introduced the $lazy_load_term_meta argument. |
| 4.5.0 | Removed the $comments_popup parameter.Introduced the $comment_status and $ping_status parameters.Introduced RAND(x) syntax for $orderby, which allows an integer seed value to random sorts. |
| 4.4.0 | Introduced $post_name__in and $title parameters. $s was updated to support excluded search terms, by prepending a hyphen. |
| 4.2.0 | Introduced the ability to order by specific clauses of a $meta_query, by passing the clause’s array key to $orderby. |
| 1.5.0 | Introduced. |