diff --git a/src/wp-includes/query.php b/src/wp-includes/query.php index fa59b0f55f..23c5d7637f 100644 --- a/src/wp-includes/query.php +++ b/src/wp-includes/query.php @@ -3123,11 +3123,12 @@ class WP_Query { if ( !$page ) $page = 1; - if ( empty($q['offset']) ) { - $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; - } else { // we're ignoring $page and using 'offset' - $q['offset'] = absint($q['offset']); + // If 'offset' is provided, it takes precedence over 'paged'. + if ( isset( $q['offset'] ) && is_numeric( $q['offset'] ) ) { + $q['offset'] = absint( $q['offset'] ); $pgstrt = $q['offset'] . ', '; + } else { + $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; } $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page']; } diff --git a/tests/phpunit/tests/query.php b/tests/phpunit/tests/query.php index 4bbf882d65..a7c20a9d2b 100644 --- a/tests/phpunit/tests/query.php +++ b/tests/phpunit/tests/query.php @@ -459,4 +459,42 @@ class Tests_Query extends WP_UnitTestCase { public function filter_parse_query_to_modify_queried_post_id( $query ) { $post = get_queried_object(); } + + /** + * @ticket 34060 + */ + public function test_offset_0_should_override_page() { + $q = new WP_Query( array( + 'paged' => 2, + 'posts_per_page' => 5, + 'offset' => 0, + ) ); + + $this->assertContains( 'LIMIT 0, 5', $q->request ); + } + + /** + * @ticket 34060 + */ + public function test_offset_should_be_ignored_when_not_set() { + $q = new WP_Query( array( + 'paged' => 2, + 'posts_per_page' => 5, + ) ); + + $this->assertContains( 'LIMIT 5, 5', $q->request ); + } + + /** + * @ticket 34060 + */ + public function test_offset_should_be_ignored_when_passed_a_non_numeric_value() { + $q = new WP_Query( array( + 'paged' => 2, + 'posts_per_page' => 5, + 'offset' => '', + ) ); + + $this->assertContains( 'LIMIT 5, 5', $q->request ); + } }