wppaste
WordPress

build_query_vars_from_query_block( WP_Block $block, int $page ): array

Since
5.8.0, 6.1.0, 6.7.0
Source
wp-includes/blocks.php:2606
Helper function that constructs a WP_Query args array from a Query block properties.

Description

It's used in Query Loop, Query Pagination Numbers and Query Pagination Next blocks.

Compatibility

WordPress
since 6.7.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
Moderate

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Constant

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

Instructions
3

Executed per call on PHP 8.5. The body compiles to 413.

Plugin surface
1 hook

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

Called by
6

6 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 callbacksapply_filters()called directly
  • cacheobject cachewp_cache_get()one call below build_query_vars_from_query_block()
  • serializeserialisationmaybe_unserialize()one call below build_query_vars_from_query_block()

Further down the call graph this can also reach query and transient. Those are 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.

WhenInstructionsCalls it makes
always3none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev413340
8.5413340
8.44133407 fewer instructions than PHP 8.3
8.3420340
8.2420340
8.1420340
7.4420340

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:

  1. apply_filters( query_loop_block_query_vars )filterline 2809 (+203 into the body)

    Filters the arguments which will be passed to `WP_Query` for the Query Loop Block.

Uses · 7

Used by · 6

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 before hand 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 (			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 );		}		if ( ! empty( $block->context['query']['taxQuery'] ) ) {			$tax_query = array();			foreach ( $block->context['query']['taxQuery'] as $taxonomy => $terms ) {				if ( is_taxonomy_viewable( $taxonomy ) && ! empty( $terms ) ) {					$tax_query[] = array(

Changelog

Introduced in 5.8.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.7.0
Added support for the format property in query.from the docblock
6.1.0
Added query_loop_block_query_vars filter and parents support in query.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.8.8 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.