Ticket #10964: query.php.r17522.diff
File query.php.r17522.diff, 7.2 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 … … 1893 1902 $join = ''; 1894 1903 $search = ''; 1895 1904 $groupby = ''; 1896 $fields = ''; 1905 $fields = "$wpdb->posts.ID"; // See #10964 1906 $quick_fields = "$wpdb->posts.*"; 1897 1907 $post_status_join = false; 1898 1908 $page = 1; 1899 1909 … … 2514 2524 2515 2525 $orderby = $q['orderby']; 2516 2526 2527 // Set up quick_* defaults from the standard placeholders, before filtering 2528 $quick_distinct = $distinct; 2529 $quick_where = $where; 2530 $quick_join = $join; 2531 $quick_groupby = $groupby; 2532 2517 2533 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2534 $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' ); 2518 2535 2519 2536 // Apply post-paging filters on where and join. Only plugins that 2520 2537 // manipulate paging queries should use these hooks. … … 2531 2548 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 2532 2549 foreach ( $pieces as $piece ) 2533 2550 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2551 2552 // And again for the quick clauses 2553 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) ); 2554 foreach ( $quick_pieces as $piece ) 2555 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2534 2556 } 2535 2557 2536 2558 // Announce current selection parameters. For use by caching plugins. … … 2550 2572 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 2551 2573 foreach ( $pieces as $piece ) 2552 2574 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2575 2576 // And again for the quick clauses 2577 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) ); 2578 foreach ( $quick_pieces as $piece ) 2579 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2553 2580 } 2554 2581 2555 2582 if ( ! empty($groupby) ) 2556 2583 $groupby = 'GROUP BY ' . $groupby; 2584 if ( ! empty($quick_groupby) ) 2585 $quick_groupby = 'GROUP BY ' . $quick_groupby; 2557 2586 if ( !empty( $orderby ) ) 2558 2587 $orderby = 'ORDER BY ' . $orderby; 2559 2588 … … 2561 2590 if ( !$q['no_found_rows'] && !empty($limits) ) 2562 2591 $found_rows = 'SQL_CALC_FOUND_ROWS'; 2563 2592 2564 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2565 if ( !$q['suppress_filters'] ) 2566 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) ); 2593 if ( empty($limits) ) { 2594 // Do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause 2595 $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby"; 2596 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2567 2597 2568 if ( 'ids' == $q['fields'] ) {2569 $this->posts = $wpdb->get_col($this->request);2598 if ( 'ids' == $q['fields'] ) { 2599 $this->posts = $wpdb->get_col($this->request); 2570 2600 2571 return $this->posts;2572 }2601 return $this->posts; 2602 } 2573 2603 2574 if ( 'id=>parent' == $q['fields'] ) {2575 $this->posts = $wpdb->get_results($this->request);2604 if ( 'id=>parent' == $q['fields'] ) { 2605 $this->posts = $wpdb->get_results($this->request); 2576 2606 2577 $r = array();2578 foreach ( $this->posts as $post )2579 $r[ $post->ID ] = $post->post_parent;2607 $r = array(); 2608 foreach ( $this->posts as $post ) 2609 $r[ $post->ID ] = $post->post_parent; 2580 2610 2581 return $r; 2611 return $r; 2612 } 2613 2614 if ( !$q['suppress_filters'] ) { 2615 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) ); 2616 $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) ); 2617 } 2618 2619 $this->posts = $wpdb->get_results($this->quick_request); 2620 2621 $this->found_posts = count($this->posts); 2622 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2623 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2624 } else { 2625 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2626 if ( !$q['suppress_filters'] ) 2627 $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) ); 2628 2629 $post_ids = $wpdb->get_col($this->request); 2630 2631 if ( empty($post_ids) ) { 2632 $this->found_posts = 0; 2633 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2634 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2635 2636 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2637 if ( !$q['suppress_filters'] ) 2638 $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) ); 2639 2640 $this->posts = array(); // no point in querying, since there are no posts 2641 } else { 2642 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 2643 $this->found_posts = $wpdb->get_var( $found_posts_query ); 2644 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2645 $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] ); 2646 2647 $post_ids = implode(',', $post_ids); 2648 $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 ) "; 2649 2650 if ( !$q['suppress_filters'] ) 2651 $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) ); 2652 2653 $this->posts = $wpdb->get_results($this->quick_request); 2654 } 2582 2655 } 2583 2656 2584 $this->posts = $wpdb->get_results($this->request);2585 2586 2657 // Raw results filter. Prior to status checks. 2587 2658 if ( !$q['suppress_filters'] ) 2588 2659 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) ); … … 2600 2671 $this->comment_count = count($this->comments); 2601 2672 } 2602 2673 2603 if ( !$q['no_found_rows'] && !empty($limits) ) {2604 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );2605 $this->found_posts = $wpdb->get_var( $found_posts_query );2606 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );2607 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);2608 }2609 2610 2674 // Check post status to determine if post should be displayed. 2611 2675 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { 2612 2676 $status = get_post_status($this->posts[0]);