build_query_vars_from_query_block( WP_Block $block, int $page ): array
- Since
- 5.8.0, 6.1.0, 6.7.0, 7.0.0, 7.1.0
- Source
wp-includes/blocks.php:2820
Query block properties.Description
It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks.
Compatibility
- WordPress
- since 7.1.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
$blockWP_Block- Block instance.
$pageint- Current query's page.
Return value
array- Returns the constructed WP_Query arguments.
Performance profile
How much work a call to build_query_vars_from_query_block() 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
- 3
- Plugin surface
- 1 hook
- Called by
- 6
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. The body compiles to 505.
Third-party callbacks on 'query_loop_block_query_vars' run inside this call, and their cost is not bounded by anything here.
6 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
apply_filters()called directly - cacheobject cache
wp_cache_get()one call below build_query_vars_from_query_block() - serializeserialisation
maybe_unserialize()one call below build_query_vars_from_query_block() - querycontent query
get_post()one call below build_query_vars_from_query_block()
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 · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost build_query_vars_from_query_block() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 3 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 505 | 3 | 53 | |
| 8.5 | 505 | 3 | 53 | |
| 8.4 | 505 | 3 | 53 | 7 fewer instructions than PHP 8.3 |
| 8.3 | 512 | 3 | 53 | |
| 8.2 | 512 | 3 | 53 | |
| 8.1 | 512 | 3 | 53 | 2 fewer instructions than PHP 7.4 |
| 7.4 | 514 | 3 | 53 |
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 build_query_vars_from_query_block() runs, in this order:
- apply_filters( query_loop_block_query_vars )filterline 3071 (+251 into the body)
Filters the arguments which will be passed to `WP_Query` for the Query Loop Block.
Uses · 8
- is_post_type_viewable()Determines whether a post type is considered "viewable".
- get_option()Retrieves an option value based on an option name.
- get_the_ID()Retrieves the ID of the current item in the WordPress Loop.
- absint()Converts a value to non-negative integer.
- is_taxonomy_viewable()Determines whether a taxonomy is considered "viewable".
- get_post_format_slugs()Retrieves the array of post format slugs.
- is_post_type_hierarchical()Determines whether the post type is hierarchical.
- apply_filters()Calls the callback functions that have been added to a filter hook.
Used by · 6
- render_block_core_post_template()Renders the `core/post-template` block on the server.
- render_block_core_query_no_results()Renders the `core/query-no-results` block on the server.
- render_block_core_query_pagination_next()Renders the `core/query-pagination-next` block on the server.
- render_block_core_query_pagination_numbers()Renders the `core/query-pagination-numbers` block on the server.
- render_block_core_query_pagination_previous()Renders the `core/query-pagination-previous` block on the server.
- render_block_core_query_total()Renders the `query-total` block on the server.
Source code
function build_query_vars_from_query_block( $block, $page ) { $query = array( 'post_type' => 'post', 'order' => 'DESC', 'orderby' => 'date', 'post__not_in' => array(), 'tax_query' => array(), ); if ( isset( $block->context['query'] ) ) { if ( ! empty( $block->context['query']['postType'] ) ) { $post_type_param = $block->context['query']['postType']; if ( is_post_type_viewable( $post_type_param ) ) { $query['post_type'] = $post_type_param; } } if ( isset( $block->context['query']['sticky'] ) && ! empty( $block->context['query']['sticky'] ) ) { $sticky = get_option( 'sticky_posts' ); if ( 'only' === $block->context['query']['sticky'] ) { /* * Passing an empty array to post__in will return have_posts() as true (and all posts will be returned). * Logic should be used beforehand to determine if WP_Query should be used in the event that the array * being passed to post__in is empty. * * @see https://core.trac.wordpress.org/ticket/28099 */ $query['post__in'] = ! empty( $sticky ) ? $sticky : array( 0 ); $query['ignore_sticky_posts'] = 1; } elseif ( 'exclude' === $block->context['query']['sticky'] ) { $query['post__not_in'] = array_merge( $query['post__not_in'], $sticky ); } elseif ( 'ignore' === $block->context['query']['sticky'] ) { $query['ignore_sticky_posts'] = 1; } } if ( ! empty( $block->context['query']['exclude'] ) ) { $excluded_post_ids = array_map( 'intval', $block->context['query']['exclude'] ); $excluded_post_ids = array_filter( $excluded_post_ids ); $query['post__not_in'] = array_merge( $query['post__not_in'], $excluded_post_ids ); } if ( ! empty( $block->context['query']['excludeCurrent'] ) ) { $current_post_id = get_the_ID(); if ( $current_post_id ) { $query['post__not_in'][] = $current_post_id; } } if ( isset( $block->context['query']['perPage'] ) && is_numeric( $block->context['query']['perPage'] ) ) { $per_page = absint( $block->context['query']['perPage'] ); $offset = 0; if ( isset( $block->context['query']['offset'] ) && is_numeric( $block->context['query']['offset'] ) ) { $offset = absint( $block->context['query']['offset'] ); } $query['offset'] = ( $per_page * ( $page - 1 ) ) + $offset; $query['posts_per_page'] = $per_page; } // Migrate `categoryIds` and `tagIds` to `tax_query` for backwards compatibility. if ( ! empty( $block->context['query']['categoryIds'] ) || ! empty( $block->context['query']['tagIds'] ) ) { $tax_query_back_compat = array(); if ( ! empty( $block->context['query']['categoryIds'] ) ) { $tax_query_back_compat[] = array( 'taxonomy' => 'category', 'terms' => array_filter( array_map( 'intval', $block->context['query']['categoryIds'] ) ), 'include_children' => false, ); } if ( ! empty( $block->context['query']['tagIds'] ) ) { $tax_query_back_compat[] = array( 'taxonomy' => 'post_tag', 'terms' => array_filter( array_map( 'intval', $block->context['query']['tagIds'] ) ), 'include_children' => false, ); } $query['tax_query'] = array_merge( $query['tax_query'], $tax_query_back_compat );Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
excludeCurrent property in query.from the docblocktaxQuery structure.from the docblockformat property in query.from the docblockquery_loop_block_query_vars filter and parents support in query.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/blocks.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.