Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions inc/namespace.php
Original file line number Diff line number Diff line change
Expand Up @@ -665,26 +665,38 @@ function action_pre_get_posts( WP_Query $query ) : void {
* @param WP_Post[]|null $posts Array of post objects. Passed by reference.
* @param WP_Query $query The WP_Query instance.
*/
add_filter( 'posts_pre_query', function( ?array $posts, WP_Query $query ) use ( &$stored_values, $user_ids ) : ?array {
$restore_query_vars = static function( ?array $posts, WP_Query $filtered_query ) use ( &$restore_query_vars, &$stored_values, $user_ids, $query ) : ?array {
if ( $filtered_query !== $query ) {
return $posts;
}

remove_filter( 'posts_pre_query', $restore_query_vars, 999 );

if ( empty( $stored_values ) ) {
return $posts;
}

// Reset the query vars to their original values.
foreach ( $stored_values as $concern => $value ) {
$query->set( $concern, $value );
$filtered_query->set( $concern, $value );
}

// Specifically set `author` when `author_name` is in use as WP_Query also sets `author` internally.
if ( ! empty( $stored_values['author_name'] ) ) {
$query->set( 'author', $user_ids[0] );
if (
array_key_exists( 'author_name', $stored_values )
&& is_string( $stored_values['author_name'] )
&& '' !== $stored_values['author_name']
) {
$filtered_query->set( 'author', $user_ids[0] );
}

// Clear the recorded values so subsequent queries are not affected.
$stored_values = [];

return $posts;
}, 999, 2 );
};

add_filter( 'posts_pre_query', $restore_query_vars, 999, 2 );
}

/**
Expand Down
47 changes: 47 additions & 0 deletions tests/phpunit/test-wp-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,4 +336,51 @@ public function testSubsequentQueriesAreUnaffected() : void {
$query2->get( 'author' )
);
}

public function testAuthorFilteredQueriesDoNotAccumulatePostsPreQueryCallbacks() : void {
$factory = self::factory()->post;

$factory->create_and_get( [
POSTS_PARAM => [
self::$users['editor']->ID,
],
] );

$before = $this->countHookCallbacks( 'posts_pre_query' );

for ( $i = 0; $i < 3; $i++ ) {
$query = new WP_Query();
$query->query( [
'post_type' => 'post',
'author' => self::$users['editor']->ID,
'fields' => 'ids',
] );
}

$after = $this->countHookCallbacks( 'posts_pre_query' );

$this->assertSame( $before, $after );
}

/**
* Count total callbacks registered for a hook across all priorities.
*
* @param string $hook Hook name.
* @return int
*/
private function countHookCallbacks( string $hook ) : int {
global $wp_filter;

if ( ! isset( $wp_filter[ $hook ] ) || ! $wp_filter[ $hook ] instanceof \WP_Hook ) {
return 0;
}

$count = 0;

foreach ( $wp_filter[ $hook ]->callbacks as $callbacks ) {
$count += count( $callbacks );
}

return $count;
}
}