Skip to content

Commit

Permalink
Translate a 'paged' string to int if passed to Query_Args
Browse files Browse the repository at this point in the history
If we use Query_Args::merge_query and the current URL is /page/<number> the
"paged" value is passed as a string and causes a fatal error.
  • Loading branch information
lipemat committed Aug 26, 2024
1 parent 70ea37d commit 2271b25
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 10 additions & 0 deletions dev/wp-unit/tests/Query/ArgsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public function test_merge_query(): void {
$this->assertEquals( $previous, $query->query_vars );
}


public function test_string_paged(): void {
$query = new \WP_Query();
$query->parse_query( [
'paged' => '2',
] );
$args = new Query_Args( [] );
$args->merge_query( $query );
$this->assertEquals( 2, $args->get_args()['paged'] );
}
}
6 changes: 5 additions & 1 deletion src/Query/Query_Args.php
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,11 @@ public function orderby( array|string $orderby, string $order = '' ): void {
public function merge_query( \WP_Query $query ): void {
foreach ( $query->query as $arg => $value ) {
if ( '' !== $value && \property_exists( $this, $arg ) && ! isset( $this->{$arg} ) ) {
$this->{$arg} = $value;
if ( 'paged' === $arg ) {
$this->{$arg} = (int) $value;
} else {
$this->{$arg} = $value;
}
}
}
$local_args = $this->get_args();
Expand Down

0 comments on commit 2271b25

Please sign in to comment.