Ticket #10964: query.php_2012-01-05.patch
File query.php_2012-01-05.patch, 11.6 KB (added by , 11 years ago) |
---|
-
query.php
1949 1949 $join = ''; 1950 1950 $search = ''; 1951 1951 $groupby = ''; 1952 $fields = '';1952 $fields = "$wpdb->posts.ID"; 1953 1953 $post_status_join = false; 1954 1954 $page = 1; 1955 $orderby = ''; 1956 $quick_fields = "$wpdb->posts.*"; 1955 1957 1956 1958 if ( isset( $q['caller_get_posts'] ) ) { 1957 1959 _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) ); … … 2033 2035 switch ( $q['fields'] ) { 2034 2036 case 'ids': 2035 2037 $fields = "$wpdb->posts.ID"; 2038 $quick_fields = "$wpdb->posts.ID"; 2036 2039 break; 2037 2040 case 'id=>parent': 2038 2041 $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; 2042 $quick_fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; 2039 2043 break; 2040 2044 default: 2041 $fields = "$wpdb->posts.*"; 2045 $fields = "$wpdb->posts.ID"; 2046 $quick_fields = "$wpdb->posts.*"; 2042 2047 } 2043 2048 2044 2049 // If a month is specified in the querystring, load that month … … 2564 2569 2565 2570 $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' ); 2566 2571 2572 // Set up quick_* defaults from the standard placeholders, before filtering 2573 $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' ); 2574 $quick_distinct = $distinct; 2575 $quick_join = $join; 2576 $quick_where = $where; 2577 $quick_groupby = $groupby; 2578 2567 2579 // Apply post-paging filters on where and join. Only plugins that 2568 2580 // manipulate paging queries should use these hooks. 2569 2581 if ( !$q['suppress_filters'] ) { 2570 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) ); 2571 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this ) ); 2572 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this ) ); 2582 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this, false ) ); 2583 $quick_distinct = apply_filters_ref_array( 'posts_distinct', array( $quick_distinct, &$this, true ) ); 2584 2585 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this, false ) ); 2586 $quick_fields = apply_filters_ref_array( 'posts_fields', array( $quick_fields, &$this, true ) ); 2587 2588 $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this, false ) ); 2589 $quick_join = apply_filters_ref_array( 'posts_join_paged', array( $quick_join, &$this, true ) ); 2590 2591 $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this, false ) ); 2592 $quick_where = apply_filters_ref_array( 'posts_where_paged', array( $quick_where, &$this, true ) ); 2593 2594 $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this, false ) ); 2595 $quick_groupby = apply_filters_ref_array( 'posts_groupby', array( $quick_groupby, &$this, true ) ); 2596 2573 2597 $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) ); 2574 $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this ) ); 2598 2575 2599 $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) ); 2576 $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this ) ); 2577 2600 2578 2601 // Filter all clauses at once, for convenience 2579 2602 $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) ); 2580 2603 foreach ( $pieces as $piece ) 2581 2604 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2605 2606 // And again for the quick clauses 2607 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) ); 2608 foreach ( $quick_pieces as $piece ) 2609 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2582 2610 } 2583 2611 2584 2612 // Announce current selection parameters. For use by caching plugins. … … 2586 2614 2587 2615 // Filter again for the benefit of caching plugins. Regular plugins should use the hooks above. 2588 2616 if ( !$q['suppress_filters'] ) { 2589 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this ) ); 2590 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this ) ); 2591 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this ) ); 2617 2618 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this, false ) ); 2619 $quick_distinct = apply_filters_ref_array( 'posts_distinct_request', array( $quick_distinct, &$this, true ) ); 2620 2621 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this, false ) ); 2622 $quick_fields = apply_filters_ref_array( 'posts_fields_request', array( $quick_fields, &$this, true ) ); 2623 2624 $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this, false ) ); 2625 $quick_join = apply_filters_ref_array( 'posts_join_request', array( $quick_join, &$this, true ) ); 2626 2627 $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this, false ) ); 2628 $quick_where = apply_filters_ref_array( 'posts_where_request', array( $quick_where, &$this, true ) ); 2629 2630 $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this, false ) ); 2631 $quick_groupby = apply_filters_ref_array( 'posts_groupby_request', array( $quick_groupby, &$this, true ) ); 2632 2592 2633 $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) ); 2593 $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this ) );2594 $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this ) );2595 2634 $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) ); 2596 2635 2597 2636 // Filter all clauses at once, for convenience 2598 2637 $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) ); 2599 2638 foreach ( $pieces as $piece ) 2600 2639 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2640 2641 // And again for the quick clauses 2642 $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) ); 2643 foreach ( $quick_pieces as $piece ) 2644 $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 2601 2645 } 2602 2646 2603 2647 if ( ! empty($groupby) ) 2604 2648 $groupby = 'GROUP BY ' . $groupby; 2649 if ( ! empty($quick_groupby) ) 2650 $quick_groupby = 'GROUP BY ' . $quick_groupby; 2605 2651 if ( !empty( $orderby ) ) 2606 2652 $orderby = 'ORDER BY ' . $orderby; 2607 2653 … … 2609 2655 if ( !$q['no_found_rows'] && !empty($limits) ) 2610 2656 $found_rows = 'SQL_CALC_FOUND_ROWS'; 2611 2657 2612 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2613 if ( !$q['suppress_filters'] ) 2614 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) ); 2658 if ( empty($limits) ) { 2659 // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause 2660 $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby"; 2661 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2615 2662 2616 if ( 'ids' == $q['fields'] ) { 2617 $this->posts = $wpdb->get_col($this->request); 2663 if ( 'ids' == $q['fields'] ) { 2664 $this->posts = $wpdb->get_col($this->request); 2665 return $this->posts; 2666 } 2618 2667 2619 return $this->posts; 2620 } 2668 if ( 'id=>parent' == $q['fields'] ) { 2669 $this->posts = $wpdb->get_results($this->request); 2621 2670 2622 if ( 'id=>parent' == $q['fields'] ) { 2623 $this->posts = $wpdb->get_results($this->request); 2624 2625 $r = array(); 2626 foreach ( $this->posts as $post ) 2671 $r = array(); 2672 foreach ( $this->posts as $post ) 2627 2673 $r[ $post->ID ] = $post->post_parent; 2628 2674 2629 return $r; 2675 return $r; 2676 } 2677 2678 if ( !$q['suppress_filters'] ) { 2679 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this, false ) ); 2680 $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) ); 2681 } 2682 2683 $this->posts = $wpdb->get_results($this->quick_request); 2684 2685 $this->found_posts = count($this->posts); 2686 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2687 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 2688 } else { 2689 2690 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits"; 2691 if ( !$q['suppress_filters'] ) 2692 $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this, false ) ); 2693 2694 $post_ids = $wpdb->get_col($this->request); 2695 2696 if ( !$post_ids ) { 2697 $this->found_posts = 0; 2698 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2699 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 2700 2701 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby"; 2702 if ( !$q['suppress_filters'] ) 2703 $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) ); 2704 2705 $this->posts = array(); // no point in querying, since there are no posts 2706 } else { 2707 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 2708 $this->found_posts = $wpdb->get_var( $found_posts_query ); 2709 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2710 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 2711 2712 $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1 = 1 $quick_where AND $wpdb->posts.ID IN ( " . implode(',', $post_ids) . " ) $quick_groupby ORDER BY FIELD( $wpdb->posts.ID, " . implode(',', $post_ids) . ") "; 2713 2714 if ( !$q['suppress_filters'] ) 2715 $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) ); 2716 2717 $this->posts = $wpdb->get_results($this->quick_request); 2718 } 2630 2719 } 2631 2720 2632 $this->posts = $wpdb->get_results($this->request);2633 2634 2721 // Raw results filter. Prior to status checks. 2635 2722 if ( !$q['suppress_filters'] ) 2636 2723 $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) ); … … 2647 2734 $this->comments = $wpdb->get_results($comments_request); 2648 2735 $this->comment_count = count($this->comments); 2649 2736 } 2650 2651 if ( !$q['no_found_rows'] && !empty($limits) ) { 2652 $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 2653 $this->found_posts = $wpdb->get_var( $found_posts_query ); 2654 $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 2655 $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 2656 } 2657 2737 2658 2738 // Check post status to determine if post should be displayed. 2659 2739 if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) { 2660 2740 $status = get_post_status($this->posts[0]);