Ticket #35476: 35476.3.diff
| File 35476.3.diff, 3.2 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/query.php
3531 3531 } 3532 3532 3533 3533 if ( 'ids' == $q['fields'] ) { 3534 3535 /** 3536 * Filter the query results when 'fields' specified in a query. 3537 * 3538 * Plugins can bypass a 'fields' query by returning 3539 * an array of posts to be returned from the query. 3540 * 3541 * @since 4.5.0 3542 * 3543 * @param array $posts The filtered array of posts. 3544 * @param array $request The complete SQL query. 3545 * @param string $fields The query 'fields' value. 3546 * @param WP_Query &$this The WP_Query instance (passed by reference). 3547 */ 3548 $this->posts = apply_filters_ref_array( 'fields_the_posts', array( false, $q['fields'], $this->request, &$this ) ); 3549 3550 if ( false !== $this->posts ) { 3551 return $this->posts; 3552 } 3553 3534 3554 $this->posts = $wpdb->get_col( $this->request ); 3535 3555 $this->posts = array_map( 'intval', $this->posts ); 3536 3556 $this->post_count = count( $this->posts ); … … 3540 3560 } 3541 3561 3542 3562 if ( 'id=>parent' == $q['fields'] ) { 3563 3564 /** This filter is documented in wp-includes/query.php */ 3565 $this->posts = apply_filters_ref_array( 'fields_the_posts', array( false, $q['fields'], $this->request, &$this ) ); 3566 3567 if ( false !== $this->posts ) { 3568 return $this->posts; 3569 } 3570 3543 3571 $this->posts = $wpdb->get_results( $this->request ); 3544 3572 $this->post_count = count( $this->posts ); 3545 3573 $this->set_found_posts( $q, $limits ); -
tests/phpunit/tests/query.php
492 492 493 493 $this->assertContains( 'LIMIT 5, 5', $q->request ); 494 494 } 495 496 497 /** 498 * @ticket 35476 499 * 500 * @dataProvider data_filter_query_results_with_fields 501 */ 502 public function test_filter_query_results_with_fields( $query_params ) { 503 504 // The array we're expecting back from the filter. 505 $filtered_post_ids_array = array( 1001, 1002, 1003 ); 506 507 508 // Create some posts. 509 $this->factory()->post->create_many( 10 ); 510 511 add_filter( 'fields_the_posts', array( $this, 'filter_fields_the_posts' ) ); 512 513 $query = new WP_Query( $query_params ); 514 515 remove_filter( 'fields_the_posts', array( $this, 'filter_fields_the_posts' ) ); 516 517 518 $this->assertSame( $filtered_post_ids_array, $query->posts ); 519 } 520 521 522 /** 523 * Data provider for test_filter_query_results_with_fields(). 524 * 525 * Passes the three available options for the $notify parameter and the expected email 526 * emails sent status as a bool. 527 * 528 * @return array { 529 * @type array { 530 * @type array $query_params The arguments that will be passed to WP_Query 531 * 532 * } 533 * } 534 */ 535 function data_filter_query_results_with_fields() { 536 return array( 537 array( 538 array( 539 'fields' => 'ids', 540 ), 541 ), 542 array( 543 array( 544 'fields' => 'id=>parent', 545 ), 546 ), 547 ); 548 } 549 550 551 /** 552 * Filter callback for test_filter_query_results_with_fields. 553 * 554 * @return array 555 */ 556 public function filter_fields_the_posts() { 557 return array( 1001, 1002, 1003 ); 558 } 495 559 }