Ticket #10964: query.php.r17389.nacin.diff
File query.php.r17389.nacin.diff, 6.5 KB (added by , 14 years ago) |
---|
-
wp-includes/query.php
878 878 var $request; 879 879 880 880 /** 881 * Get quick post database query. 882 * 883 * @since trunk 884 * @access public 885 * @var string 886 */ 887 var $quick_request; 888 889 /** 881 890 * List of posts. 882 891 * 883 892 * @since 1.5.0 … … 1887 1896 $join = ''; 1888 1897 $search = ''; 1889 1898 $groupby = ''; 1890 $fields = ''; 1899 $fields = "$wpdb->posts.ID"; // See #10964 1900 $quick_fields = "$wpdb->posts.*"; 1891 1901 $post_status_join = false; 1892 1902 $page = 1; 1893 1903 … … 2506 2516 2507 2517 $orderby = $q['orderby']; 2508 2518 2519 // Set up quick_* defaults from the standard placeholders, before filtering 2520 $quick_distinct = $distinct; 2521 $quick_where = $where; 2522 $quick_join = $join; 2523 $quick_groupby = $groupby; 2524 2509 2525 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2526 $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' ); 2510 2527 2511 2528 // Apply post-paging filters on where and join. Only plugins that 2512 2529 // manipulate paging queries should use these hooks. … … 2523 2540 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 2524 2541 foreach ( $pieces as $piece ) 2525 2542 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2543 2544 // And again for the quick clauses 2545 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) ); 2546 foreach ( $quick_pieces as $piece ) 2547 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2526 2548 } 2527 2549 2528 2550 // Announce current selection parameters. For use by caching plugins. … … 2542 2564 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 2543 2565 foreach ( $pieces as $piece ) 2544 2566 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2567 2568 // And again for the quick clauses 2569 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) ); 2570 foreach ( $quick_pieces as $piece ) 2571 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2545 2572 } 2546 2573 2547 2574 if ( ! empty($groupby) ) 2548 2575 $groupby = 'GROUP BY ' . $groupby; 2576 if ( ! empty($quick_groupby) ) 2577 $quick_groupby = 'GROUP BY ' . $quick_groupby; 2549 2578 if ( !empty( $orderby ) ) 2550 2579 $orderby = 'ORDER BY ' . $orderby; 2551 2580 … … 2553 2582 if ( !$q['no_found_rows'] && !empty($limits) ) 2554 2583 $found_rows = 'SQL_CALC_FOUND_ROWS'; 2555 2584 2556 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2557 if ( !$q['suppress_filters'] ) 2558 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) ); 2585 if ( empty($limits) ) { 2586 // Do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause 2587 $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby"; 2588 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2559 2589 2560 if ( 'ids' == $q['fields'] ) {2561 $this->posts = $wpdb->get_col($this->request);2590 if ( 'ids' == $q['fields'] ) { 2591 $this->posts = $wpdb->get_col($this->request); 2562 2592 2563 return $this->posts;2564 }2593 return $this->posts; 2594 } 2565 2595 2566 if ( 'id=>parent' == $q['fields'] ) {2567 $this->posts = $wpdb->get_results($this->request);2596 if ( 'id=>parent' == $q['fields'] ) { 2597 $this->posts = $wpdb->get_results($this->request); 2568 2598 2569 $r = array();2570 foreach ( $this->posts as $post )2571 $r[ $post->ID ] = $post->post_parent;2599 $r = array(); 2600 foreach ( $this->posts as $post ) 2601 $r[ $post->ID ] = $post->post_parent; 2572 2602 2573 return $r; 2603 return $r; 2604 } 2605 2606 if ( !$q['suppress_filters'] ) { 2607 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) ); 2608 $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) ); 2609 } 2610 2611 $this->posts = $wpdb->get_results($this->quick_request); 2612 2613 $this->found_posts = count($this->posts); 2614 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2615 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2616 } else { 2617 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2618 if ( !$q['suppress_filters'] ) 2619 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) ); 2620 2621 $post_ids = $wpdb->get_col($this->request); 2622 2623 if ( empty($post_ids) ) { 2624 $this->found_posts = 0; 2625 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2626 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2627 2628 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2629 if ( !$q['suppress_filters'] ) 2630 $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) ); 2631 2632 $this->posts = array(); // no point in querying, since there are no posts 2633 } else { 2634 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 2635 $this->found_posts = $wpdb->get_var( $found_posts_query ); 2636 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2637 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2638 2639 $post_ids = implode(',', $post_ids); 2640 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1 = 1 $quick_where AND $wpdb->posts.ID IN ($post_ids) $quick_groupby ORDER BY FIELD( $wpdb->posts.ID, $post_ids ) "; 2641 2642 if ( !$q['suppress_filters'] ) 2643 $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) ); 2644 2645 $this->posts = $wpdb->get_results($this->quick_request); 2646 } 2574 2647 } 2575 2648 2576 $this->posts = $wpdb->get_results($this->request);2577 2578 2649 // Raw results filter. Prior to status checks. 2579 2650 if ( !$q['suppress_filters'] ) 2580 2651 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );