wppaste
WordPress

WP_Query::parse_query( string|array $query = '' )

Since
1.5.0, 4.2.0, 4.4.0, 4.5.0, 4.6.0, 4.9.0, 5.1.0, 5.3.0, 6.1.0, 6.2.0
Source
wp-includes/class-wp-query.php:791
Parses a query string and sets query type booleans.

Compatibility

WordPress
since 6.2.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$querystring|arrayoptional
Array or string of Query parameters.Default: ''
  • $attachment_idint

    Attachment post ID. Used for 'attachment' post_type.
  • $authorint|string

    Author ID, or comma-separated list of IDs.
  • $author_namestring

    User '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_resultsbooldefault: true

    Whether to cache post information.
  • $catint|string

    Category 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_namestring

    Use category slug (not name, this or any children).
  • $comment_countarray|int

    Filter 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_statusstring

    Comment status.
  • $comments_per_pageintdefault: 'comments_per_page' option

    The number of comments to return per page.
  • $date_queryarray

    An associative array of WP_Date_Query arguments. See WP_Date_Query::</strong>construct().
  • $dayintdefault: empty. Accepts numbers 1-31

    Day of the month.
  • $exactbooldefault: false

    Whether to search by exact keyword.
  • $fieldsstringdefault: ''

    Post 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[]).
  • $hourintdefault: empty. Accepts numbers 0-23

    Hour of the day.
  • $ignore_sticky_postsint|booldefault: false

    Whether to ignore sticky posts or not. Setting this to false excludes stickies from 'post__in'. Accepts 1|true, 0|false.
  • $mintdefault: empty

    Combination YearMonth. Accepts any four-digit year and month numbers 01-12.
  • $meta_keystring|string[]

    Meta key or keys to filter by.
  • $meta_valuestring|string[]

    Meta value or values to filter by.
  • $meta_comparestring

    MySQL operator used for comparing the meta value. See WP_Meta_Query::construct() for accepted values and default value.
  • $meta_compare_keystring

    MySQL operator used for comparing the meta key. See WP_Meta_Query::</strong>construct() for accepted values and default value.
  • $meta_typestring

    MySQL 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_keystring

    MySQL data type that the meta_key column will be CAST to for comparisons. See WP_Meta_Query::</strong>construct() for accepted values and default value.
  • $meta_queryarray

    An associative array of WP_Meta_Query arguments. See WP_Meta_Query::construct() for accepted values.
  • $menu_orderint

    The menu order of the posts.
  • $minuteintdefault: empty. Accepts numbers 0-59

    Minute of the hour.
  • $monthnumintdefault: empty. Accepts numbers 1-12

    The two-digit month.
  • $namestring

    Post slug.
  • $nopagingbooldefault: false

    Show all posts (true) or paginate (false).
  • $no_found_rowsbooldefault: false

    Whether to skip counting the total rows found. Enabling can improve performance.
  • $offsetint

    The number of posts to offset before retrieval.
  • $orderstringdefault: 'DESC'. Accepts 'ASC', 'DESC'

    Designates ascending or descending order of posts.
  • $orderbystring|array

    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. To sort by a specific $meta_query clause, 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'.
  • $pint

    Post ID.
  • $pageint

    Show the number of posts that would show up on page X of a static front page.
  • $pagedint

    The number of the current page.
  • $page_idint

    Page ID.
  • $pagenamestring

    Page slug.
  • $permstring

    Show posts if user has the appropriate capability.
  • $ping_statusstring

    Ping 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_typestring

    The mime type of the post. Used for 'attachment' post_type.
  • $post_name__instring[]

    An array of post slugs that results must match.
  • $post_parentint

    Page 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[]default: 'any' if using 'tax_query'

    A post type slug (string) or array of post type slugs.
  • $post_statusstring|string[]

    A post status (string) or array of post statuses.
  • $posts_per_pageint

    The number of posts to query for. Use -1 to request all posts.
  • $posts_per_archive_pageint

    The number of posts to query for by archive page. Overrides 'posts_per_page' when is_archive(), or is_search() are true.
  • $sstring

    Search 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[]default: empty array

    Array of column names to be searched. Accepts 'post_title', 'post_excerpt' and 'post_content'.
  • $secondintdefault: empty. Accepts numbers 0-59

    Second of the minute.
  • $sentencebooldefault: false

    Whether to search by phrase.
  • $suppress_filtersbooldefault: false

    Whether to suppress filters.
  • $tagstring

    Tag 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_idint

    Tag 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_queryarray

    An associative array of WP_Tax_Query arguments. See WP_Tax_Query::__construct().
  • $titlestring

    Post title.
  • $update_post_meta_cachebooldefault: true

    Whether to update the post meta cache.
  • $update_post_term_cachebooldefault: true

    Whether to update the post term cache.
  • $update_menu_item_cachebooldefault: false

    Whether to update the menu item cache.
  • $lazy_load_term_metabool

    Whether 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.
  • $wintdefault: empty. Accepts numbers 0-53

    The week number of the year.
  • $yearintdefault: empty. Accepts any four-digit year

    The four-digit year.

Performance profile

How much work a call to WP_Query::parse_query() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Heavy

Reaches the database via get_post().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
215–252

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 842.

Plugin surface
1 hook

Third-party callbacks on 'parse_query' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • optionoption read or writeget_option()called directly
  • hookthird-party callbacksdo_action_ref_array()called directly
  • serializeserialisationserialize()called directly
  • cacheobject cachewp_cache_get()one call below WP_Query::parse_query()
  • querycontent queryget_post()one call below WP_Query::parse_query()

Further down the call graph this can also reach transient. That is the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 8 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Query::parse_query() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
empty($query) && isset($value) && empty($qv) && !is_admin() && !$qv215–237->fill_query_vars(), is_admin(), serialize(), md5()
empty($query) && isset($value) && empty($qv) && !is_admin() && !$qv217–239->fill_query_vars(), is_admin(), ->set_404(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv225–247->fill_query_vars(), is_admin(), sanitize_key(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv226–240->fill_query_vars(), is_admin(), array_map(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv227–249->fill_query_vars(), is_admin(), sanitize_key(), ->set_404(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv228–242->fill_query_vars(), is_admin(), array_map(), ->set_404(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv236–250->fill_query_vars(), is_admin(), sanitize_key(), array_map(), serialize(), md5()
empty($query) && isset($value) && !is_admin() && !$qv238–252->fill_query_vars(), is_admin(), sanitize_key(), array_map(), ->set_404(), serialize(), md5()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev835215–2521387 fewer instructions than PHP 8.5
8.5842215–252138
8.4842215–25213832 fewer instructions than PHP 8.3
8.3874221–258138
8.2874221–2581381 more instruction than PHP 8.1
8.1873221–25813841 fewer instructions than PHP 7.4
7.4914262–299138

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 1

One hook fires while WP_Query::parse_query() runs, in this order:

  1. do_action( parse_query )actionline 1140 (+349 into the body)

    Fires after the main query vars have been parsed.

Uses · 16

Show all 16

Used by · 2

Source code

	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 );		$qv                       = &$this->query_vars;		$this->query_vars_changed = true; 		if ( ! empty( $qv['robots'] ) ) {			$this->is_robots = true;		} elseif ( ! empty( $qv['favicon'] ) ) {			$this->is_favicon = true;		} 		if ( ! is_scalar( $qv['p'] ) || (int) $qv['p'] < 0 ) {			$qv['p']     = 0;			$qv['error'] = '404';		} else {			$qv['p'] = (int) $qv['p'];		} 		$qv['page_id']  = is_scalar( $qv['page_id'] ) ? absint( $qv['page_id'] ) : 0;		$qv['year']     = is_scalar( $qv['year'] ) ? absint( $qv['year'] ) : 0;		$qv['monthnum'] = is_scalar( $qv['monthnum'] ) ? absint( $qv['monthnum'] ) : 0;		$qv['day']      = is_scalar( $qv['day'] ) ? absint( $qv['day'] ) : 0;		$qv['w']        = is_scalar( $qv['w'] ) ? absint( $qv['w'] ) : 0;		$qv['m']        = is_scalar( $qv['m'] ) ? preg_replace( '|[^0-9]|', '', $qv['m'] ) : '';		$qv['paged']    = is_scalar( $qv['paged'] ) ? absint( $qv['paged'] ) : 0;		$qv['cat']      = preg_replace( '|[^0-9,-]|', '', $qv['cat'] ); // Array or comma-separated list of positive or negative integers.		$qv['author']   = is_scalar( $qv['author'] ) ? preg_replace( '|[^0-9,-]|', '', $qv['author'] ) : ''; // Comma-separated list of positive or negative integers.		$qv['pagename'] = is_scalar( $qv['pagename'] ) ? trim( $qv['pagename'] ) : '';		$qv['name']     = is_scalar( $qv['name'] ) ? trim( $qv['name'] ) : '';		$qv['title']    = is_scalar( $qv['title'] ) ? trim( $qv['title'] ) : ''; 		if ( is_scalar( $qv['hour'] ) && '' !== $qv['hour'] ) {			$qv['hour'] = absint( $qv['hour'] );		} else {			$qv['hour'] = '';		} 		if ( is_scalar( $qv['minute'] ) && '' !== $qv['minute'] ) {			$qv['minute'] = absint( $qv['minute'] );		} else {			$qv['minute'] = '';		} 		if ( is_scalar( $qv['second'] ) && '' !== $qv['second'] ) {			$qv['second'] = absint( $qv['second'] );		} else {			$qv['second'] = '';		} 		if ( is_scalar( $qv['menu_order'] ) && '' !== $qv['menu_order'] ) {			$qv['menu_order'] = absint( $qv['menu_order'] );		} else {			$qv['menu_order'] = '';		} 		// Fairly large, potentially too large, upper bound for search string lengths.		if ( ! is_scalar( $qv['s'] ) || ( ! empty( $qv['s'] ) && strlen( $qv['s'] ) > 1600 ) ) {			$qv['s'] = '';		} 		// Compat. Map subpost to attachment.		if ( is_scalar( $qv['subpost'] ) && '' != $qv['subpost'] ) {			$qv['attachment'] = $qv['subpost'];		}		if ( is_scalar( $qv['subpost_id'] ) && '' != $qv['subpost_id'] ) {			$qv['attachment_id'] = $qv['subpost_id'];		} 		$qv['attachment_id'] = is_scalar( $qv['attachment_id'] ) ? absint( $qv['attachment_id'] ) : 0; 		if ( ( '' !== $qv['attachment'] ) || ! empty( $qv['attachment_id'] ) ) {			$this->is_single     = true;

Changelog

Introduced in 6.2.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

6.2.0
Introduced the $search_columns parameter.from the docblock
6.1.0
Introduced the $update_menu_item_cache parameter.from the docblock
5.3.0
Introduced the $meta_type_key parameter.from the docblock
5.1.0
Introduced the $meta_compare_key parameter.from the docblock
4.9.0
Introduced the $comment_count parameter.from the docblock
4.6.0
Added 'post_name__in' support for $orderby. Introduced the $lazy_load_term_meta argument.from the docblock
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.from the docblock
4.4.0
Introduced $post_name__in and $title parameters. $s was updated to support excluded search terms, by prepending a hyphen.from the docblock
4.2.0
Introduced the ability to order by specific clauses of a $meta_query, by passing the clause's array key to $orderby.from the docblock
1.5.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-query.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.