Skip to content

Commit d28cc71

Browse files
committed
Query: Add resolved_query_vars property to WP_Query for external systems
1 parent e12ddb3 commit d28cc71

1 file changed

Lines changed: 55 additions & 1 deletion

File tree

src/wp-includes/class-wp-query.php

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ class WP_Query {
3434
*/
3535
public $query_vars = array();
3636

37+
/**
38+
* Resolved query vars with final values used in the SQL query.
39+
*
40+
* @since x.x.x
41+
* @var array
42+
*/
43+
public $resolved_query_vars = array();
44+
3745
/**
3846
* Taxonomy query, as passed to get_tax_sql().
3947
*
@@ -531,7 +539,8 @@ private function init_query_flags() {
531539
public function init() {
532540
unset( $this->posts );
533541
unset( $this->query );
534-
$this->query_vars = array();
542+
$this->query_vars = array();
543+
$this->resolved_query_vars = array();
535544
unset( $this->queried_object );
536545
unset( $this->queried_object_id );
537546
$this->post_count = 0;
@@ -1863,6 +1872,23 @@ public function get( $query_var, $default_value = '' ) {
18631872
return $this->query_vars[ $query_var ] ?? $default_value;
18641873
}
18651874

1875+
/**
1876+
* Retrieves the value of a resolved query variable.
1877+
*
1878+
* Resolved query vars contain the final values used in the SQL query
1879+
* after get_posts() has applied defaults and transformations.
1880+
*
1881+
* @since x.x.x
1882+
*
1883+
* @param string $query_var Query variable key.
1884+
* @param mixed $default_value Optional. Value to return if the resolved query variable is not set.
1885+
* Default empty string.
1886+
* @return mixed Contents of the resolved query variable.
1887+
*/
1888+
public function get_resolved_query_var( $query_var, $default_value = '' ) {
1889+
return $this->resolved_query_vars[ $query_var ] ?? $default_value;
1890+
}
1891+
18661892
/**
18671893
* Sets the value of a query variable.
18681894
*
@@ -3659,6 +3685,34 @@ public function get_posts() {
36593685
wp_queue_posts_for_term_meta_lazyload( $this->posts );
36603686
}
36613687

3688+
/*
3689+
* Store the resolved query variable values for external systems.
3690+
*/
3691+
$this->resolved_query_vars['post_type'] = $post_type;
3692+
3693+
if ( ! empty( $q_status ) ) {
3694+
$this->resolved_query_vars['post_status'] = $q_status;
3695+
} elseif ( ! $this->is_singular ) {
3696+
3697+
$resolved_statuses = get_post_stati( array( 'public' => true ) );
3698+
3699+
if ( $this->is_admin ) {
3700+
$resolved_statuses = array_merge(
3701+
$resolved_statuses,
3702+
get_post_stati( array( 'protected' => true, 'show_in_admin_all_list' => true ) )
3703+
);
3704+
}
3705+
3706+
if ( is_user_logged_in() && current_user_can( $read_private_cap ) ) {
3707+
$resolved_statuses = array_merge(
3708+
$resolved_statuses,
3709+
get_post_stati( array( 'private' => true ) )
3710+
);
3711+
}
3712+
3713+
$this->resolved_query_vars['post_status'] = array_unique( $resolved_statuses );
3714+
}
3715+
36623716
return $this->posts;
36633717
}
36643718

0 commit comments

Comments
 (0)