wppaste
WordPress

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

Since
1.5.0
Source
wp-includes/class-wp-query.php:3982

Builds a WP_Query object and immediately runs the query only when the $query argument is not empty, accepting either a URL-style query string or an array of query vars. Passing nothing (the default) skips the query entirely, leaving the object with no posts until you call WP_Query::query() yourself. Most theme and plugin code calls this indirectly through `new WP_Query( $args )` rather than referencing the constructor by name.

Constructor.

Description

Sets up the WordPress query, if parameter is not empty.

Compatibility

WordPress
since 1.5.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
URL query string or array of vars.Default: ''

Code examples

Every example is editable and runs in a real WordPress booted in your browser by WordPress Playground. Press Run, then edit the code: clicking away re-runs it. Nothing is sent anywhere until you do.

Run a custom loop over posts in a category

Instantiate WP_Query with an array of args to pull posts from the news category outside the main loop.

$news_query = new WP_Query( array(
	'category_name'  => 'news',
	'posts_per_page' => 5,
) );

if ( $news_query->have_posts() ) {
	while ( $news_query->have_posts() ) {
		$news_query->the_post();
		echo esc_html( get_the_title() ) . '<br>';
	}
	wp_reset_postdata();
} else {
	echo 'No posts found in the news category.';
}

wp_reset_postdata() restores the global $post after the custom loop finishes.

Query posts that have a specific meta key

Pass a meta_key argument to the constructor to find posts carrying the price meta field.

$priced_query = new WP_Query( array(
	'meta_key'       => 'price',
	'meta_compare'   => 'EXISTS',
	'posts_per_page' => -1,
) );

echo esc_html( sprintf( 'Found %d post(s) with a price meta key.', $priced_query->post_count ) );

wp_reset_postdata();

Common problems and fixes · 3

Why does new WP_Query() with no arguments return zero posts?

The constructor only calls $this->query() when $query is not empty, and the default value is an empty string. With no arguments the query never runs, so post_count stays 0 and have_posts() is always false.

Why does my sidebar loop reset the main loop's posts?

The constructor's query() call points the_post() at the new WP_Query object's own posts array, but if you forget to call wp_reset_postdata() afterward, later calls to template tags keep reading from this custom query instead of the main one.

Can I pass a URL-style query string instead of an array?

Yes, the $query parameter accepts either a string or an array, but a string is parsed like a URL query string, so array-style arguments such as meta_query or tax_query cannot be expressed that way.

Alternatives and related functions

get_posts
When you just need a plain array of post objects and don't need to loop with the_post() or manage pagination manually.
WP_Query::query
When you already have a WP_Query instance and want to re-run it with a new set of arguments instead of creating a second object.
pre_get_posts
When you want to modify WordPress's existing main query instead of instantiating a separate WP_Query object.
WP_User_Query
When the data you need to query is users rather than posts.

Performance profile

How much work a call to WP_Query::__construct() 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
Trivial

Reaches the database via ->query().

Scaling
Constant

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

Instructions
4–7

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
45

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

What it touches

  • sqldatabase query->query()called directly

What one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
empty($query)4none
!empty($query)7->query()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 7 instructions, 4–7 executed per call, 1 branch. The work does not change between versions.

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.

Uses · 1

Used by · 45

Show all 45

Source code

	public function __construct( $query = '' ) {		if ( ! empty( $query ) ) {			$this->query( $query );		}	}

Changelog

Introduced in 1.5.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.

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.