Ticket #18897: 18897.3.diff
File 18897.3.diff, 3.3 KB (added by , 9 years ago) |
---|
-
src/wp-includes/query.php
3063 3063 if ( !$page ) 3064 3064 $page = 1; 3065 3065 3066 if ( empty($q['offset']) ) { 3067 $pgstrt = absint( ( $page - 1 ) * $q['posts_per_page'] ) . ', '; 3068 } else { // we're ignoring $page and using 'offset' 3069 $q['offset'] = absint($q['offset']); 3070 $pgstrt = $q['offset'] . ', '; 3066 $pgstrt = ($page - 1) * $q['posts_per_page']; 3067 3068 if ( ! empty( $q['offset'] ) ) { 3069 $q['offset'] = intval( $q['offset'] ); 3070 $pgstrt += $q['offset']; 3071 $pgstrt = max( $pgstrt, 0 ); 3071 3072 } 3072 $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];3073 $limits = 'LIMIT ' . $pgstrt . ', ' . $q['posts_per_page']; 3073 3074 } 3074 3075 3075 3076 // Comments feeds -
tests/phpunit/tests/query/results.php
281 281 $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) ); 282 282 } 283 283 284 function test_query_negative_offset() { 285 $posts = $this->q->query('offset=-2'); 286 287 // A standalone negative offset should have no effect 288 $expected = array ( 289 0 => 'tags-a-and-c', 290 1 => 'tags-b-and-c', 291 2 => 'tags-a-and-b', 292 3 => 'tag-c', 293 4 => 'tag-b', 294 5 => 'tag-a', 295 6 => 'tags-a-b-c', 296 7 => 'raw-html-code', 297 8 => 'simple-markup-test', 298 9 => 'embedded-video', 299 ); 300 301 $this->assertCount( 10, $posts ); 302 $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) ); 303 } 304 284 305 function test_query_paged() { 285 306 $posts = $this->q->query('paged=2'); 286 307 … … 342 363 } 343 364 344 365 /** 366 * @ticket 18897 367 */ 368 function test_query_negative_offset_and_paged() { 369 // Negative offset permits a posts_per_page on page 1 to be lower 370 // than posts_per_page on subsequent pages without skipping posts. 371 // e.g. posts_per_page = 5 on page 1; posts_per_page = 10 on page 2+ 372 // For this test, post 6 through 15 should be returned. 373 $posts = $this->q->query('posts_per_page=10&paged=2&offset=-5'); 374 375 $expected = array ( 376 0 => 'tag-a', 377 1 => 'tags-a-b-c', 378 2 => 'raw-html-code', 379 3 => 'simple-markup-test', 380 4 => 'embedded-video', 381 5 => 'contributor-post-approved', 382 6 => 'one-comment', 383 7 => 'no-comments', 384 8 => 'many-trackbacks', 385 9 => 'one-trackback', 386 ); 387 388 $this->assertCount( 10, $posts ); 389 $this->assertTrue( $this->q->is_paged() ); 390 $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) ); 391 } 392 393 /** 394 * @ticket 18897 395 */ 396 function test_query_large_negative_offset_and_paged() { 397 $posts = $this->q->query('posts_per_page=10&paged=2&offset=-15'); 398 399 // Offset cannot reach before starting index 400 $expected = array ( 401 0 => 'tags-a-and-c', 402 1 => 'tags-b-and-c', 403 2 => 'tags-a-and-b', 404 3 => 'tag-c', 405 4 => 'tag-b', 406 5 => 'tag-a', 407 6 => 'tags-a-b-c', 408 7 => 'raw-html-code', 409 8 => 'simple-markup-test', 410 9 => 'embedded-video', 411 ); 412 413 $this->assertCount( 10, $posts ); 414 $this->assertTrue( $this->q->is_paged() ); 415 $this->assertEquals( $expected, wp_list_pluck( $posts, 'post_name' ) ); 416 } 417 418 /** 345 419 * @ticket 11056 346 420 */ 347 421 function test_query_post_parent__in() {