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
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_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_resultsbooldefault: trueWhether to cache post information.$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_pageintdefault: 'comments_per_page' optionThe number of comments to return per page.$date_queryarrayAn associative array of WP_Date_Query arguments. See WP_Date_Query::</strong>construct().$dayintdefault: empty. Accepts numbers 1-31Day of the month.$exactbooldefault: falseWhether 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-23Hour of the day.$ignore_sticky_postsint|booldefault: falseWhether to ignore sticky posts or not. Setting this to false excludes stickies from 'post__in'. Accepts 1|true, 0|false.$mintdefault: emptyCombination 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_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::</strong>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::</strong>construct() for accepted values and default value.$meta_queryarrayAn associative array of WP_Meta_Query arguments. See WP_Meta_Query::construct() for accepted values.$menu_orderintThe menu order of the posts.$minuteintdefault: empty. Accepts numbers 0-59Minute of the hour.$monthnumintdefault: empty. Accepts numbers 1-12The two-digit month.$namestringPost slug.$nopagingbooldefault: falseShow all posts (true) or paginate (false).$no_found_rowsbooldefault: falseWhether to skip counting the total rows found. Enabling can improve performance.$offsetintThe number of posts to offset before retrieval.$orderstringdefault: 'DESC'. Accepts 'ASC', 'DESC'Designates ascending or descending order of posts.$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[]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_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[]default: empty arrayArray of column names to be searched. Accepts 'post_title', 'post_excerpt' and 'post_content'.$secondintdefault: empty. Accepts numbers 0-59Second of the minute.$sentencebooldefault: falseWhether to search by phrase.$suppress_filtersbooldefault: falseWhether to suppress filters.$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_queryarrayAn associative array of WP_Tax_Query arguments. See WP_Tax_Query::__construct().$titlestringPost title.$update_post_meta_cachebooldefault: trueWhether to update the post meta cache.$update_post_term_cachebooldefault: trueWhether to update the post term cache.$update_menu_item_cachebooldefault: falseWhether to update the menu item cache.$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.$wintdefault: empty. Accepts numbers 0-53The week number of the year.$yearintdefault: empty. Accepts any four-digit yearThe 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
- Scaling
- Constant
- Instructions
- 215–252
- Plugin surface
- 1 hook
- Called by
- 2
Reaches the database via get_post().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 842.
Third-party callbacks on 'parse_query' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- optionoption read or write
get_option()called directly - hookthird-party callbacks
do_action_ref_array()called directly - serializeserialisation
serialize()called directly - cacheobject cache
wp_cache_get()one call below WP_Query::parse_query() - querycontent query
get_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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($query) && isset($value) && empty($qv) && !is_admin() && !$qv | 215–237 | ->fill_query_vars(), is_admin(), serialize(), md5() |
empty($query) && isset($value) && empty($qv) && !is_admin() && !$qv | 217–239 | ->fill_query_vars(), is_admin(), ->set_404(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 225–247 | ->fill_query_vars(), is_admin(), sanitize_key(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 226–240 | ->fill_query_vars(), is_admin(), array_map(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 227–249 | ->fill_query_vars(), is_admin(), sanitize_key(), ->set_404(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 228–242 | ->fill_query_vars(), is_admin(), array_map(), ->set_404(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 236–250 | ->fill_query_vars(), is_admin(), sanitize_key(), array_map(), serialize(), md5() |
empty($query) && isset($value) && !is_admin() && !$qv | 238–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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 835 | 215–252 | 138 | 7 fewer instructions than PHP 8.5 |
| 8.5 | 842 | 215–252 | 138 | |
| 8.4 | 842 | 215–252 | 138 | 32 fewer instructions than PHP 8.3 |
| 8.3 | 874 | 221–258 | 138 | |
| 8.2 | 874 | 221–258 | 138 | 1 more instruction than PHP 8.1 |
| 8.1 | 873 | 221–258 | 138 | 41 fewer instructions than PHP 7.4 |
| 7.4 | 914 | 262–299 | 138 |
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:
- do_action( parse_query )actionline 1140 (+349 into the body)
Fires after the main query vars have been parsed.
Uses · 16
- wp_parse_args()Merges user defined arguments into defaults array.
- absint()Converts a value to non-negative integer.
- wp_checkdate()Tests if the supplied date is valid for the Gregorian calendar.
- get_post_type_object()Retrieves a post type object by name.
- is_admin()Determines whether the current request is for an administrative interface page.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- wp_is_serving_rest_request()Determines whether WordPress is currently serving a REST API request.
- get_option()Retrieves an option value based on an option name.
- get_page_by_path()Retrieves a page given its path.
- sanitize_key()Sanitizes a string key.
- do_action_ref_array()Calls the callback functions that have been added to an action hook, specifying arguments in an array.
- WP_Query::init()Initiates object properties and sets default values.
Show all 16
- WP_Query::fill_query_vars()Fills in the query variables, which do not exist within the parameter.
- WP_Query::parse_tax_query()Parses various taxonomy related query vars.
- WP_Query::is_main_query()Determines whether the query is the main query.
- WP_Query::set_404()Sets the 404 property and saves whether query is feed.
Used by · 2
- WP_Query::get_posts()Retrieves an array of posts based on query variables.
- WP_Query::parse_query_vars()Reparses the query vars.
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.
Signature, return type and hooks compared across 5 parsed releases.
$search_columns parameter.from the docblock$update_menu_item_cache parameter.from the docblock$meta_type_key parameter.from the docblock$meta_compare_key parameter.from the docblock$comment_count parameter.from the docblock$orderby. Introduced the $lazy_load_term_meta argument.from the docblock$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$post_name__in and $title parameters. $s was updated to support excluded search terms, by prepending a hyphen.from the docblock$meta_query, by passing the clause's array key to $orderby.from the docblockAbout 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.