Make WordPress Core

Ticket #5358: 5358.2.diff

File 5358.2.diff, 5.8 KB (added by kovshenin, 11 years ago)
  • wp-includes/query.php

     
    5858}
    5959
    6060/**
     61 * Retrieve the currently-queried objects. Wrapper for $wp_query->get_queried_objects()
     62 *
     63 * @uses WP_Query::get_queried_objects
     64 *
     65 * @since 3.7.0
     66 * @access public
     67 *
     68 * @return object|null
     69 */
     70function get_queried_objects() {
     71        global $wp_query;
     72        return $wp_query->get_queried_objects();
     73}
     74
     75/**
     76 * Retrieve IDs of the current queried objects. Wrapper for $wp_query->get_queried_object_ids()
     77 *
     78 * @uses WP_Query::get_queried_object_ids()
     79 *
     80 * @since 3.7.0
     81 * @access public
     82 *
     83 * @return int
     84 */
     85function get_queried_object_ids() {
     86        global $wp_query;
     87        return $wp_query->get_queried_object_ids();
     88}
     89
     90/**
    6191 * Set query variable.
    6292 *
    6393 * @see WP_Query::set()
     
    896926        var $queried_object_id;
    897927
    898928        /**
     929         * Holds the data for the objects that are queried.
     930         *
     931         * @since 3.7.0
     932         * @access public
     933         * @var array
     934         */
     935        var $queried_objects;
     936
     937        /**
     938         * The IDs of the queried objects.
     939         *
     940         * @since 3.7.0
     941         * @access public
     942         * @var array
     943         */
     944        var $queried_object_ids;
     945
     946        /**
    899947         * Get post database query.
    900948         *
    901949         * @since 2.0.1
     
    30273075         * @return object
    30283076         */
    30293077        function get_queried_object() {
    3030                 if ( isset($this->queried_object) )
    3031                         return $this->queried_object;
     3078                $this->get_queried_objects();
     3079                return $this->queried_object;
     3080        }
    30323081
    3033                 $this->queried_object = null;
    3034                 $this->queried_object_id = 0;
     3082        /**
     3083         * Retrieve ID of the current queried object.
     3084         *
     3085         * @since 1.5.0
     3086         * @access public
     3087         *
     3088         * @return int
     3089         */
     3090        function get_queried_object_id() {
     3091                $this->get_queried_objects();
     3092                return $this->queried_object_id;
     3093        }
    30353094
     3095        /**
     3096         * Retrieve queried objects.
     3097         *
     3098         * If queried objects is not set, then the queried object will be set from
     3099         * the categories, tags, taxonomies, posts page, single post, page, or author
     3100         * query variable. After it is set up, it will be returned.
     3101         *
     3102         * @since 3.7.0
     3103         * @access public
     3104         *
     3105         * @return array
     3106         */
     3107        public function get_queried_objects() {
     3108                if ( isset( $this->queried_objects ) )
     3109                        return $this->queried_objects;
     3110
     3111                $this->queried_objects = array();
     3112                $this->queried_object_ids = array();
     3113
    30363114                if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    30373115                        $tax_query_in_and = wp_list_filter( $this->tax_query->queries, array( 'operator' => 'NOT IN' ), 'NOT' );
    30383116
     3117                        $terms = array();
    30393118                        $query = reset( $tax_query_in_and );
    30403119
    3041                         if ( 'term_id' == $query['field'] )
    3042                                 $term = get_term( reset( $query['terms'] ), $query['taxonomy'] );
    3043                         elseif ( $query['terms'] )
    3044                                 $term = get_term_by( $query['field'], reset( $query['terms'] ), $query['taxonomy'] );
     3120                        if ( $query['terms'] ) {
     3121                                foreach ( $query['terms'] as $term ) {
     3122                                        $term = get_term_by( $query['field'], $term, $query['taxonomy'] );
     3123                                        if ( $term && ! is_wp_error( $term ) ) {
     3124                                                $terms[] = $term;
     3125                                        }
     3126                                }
     3127                        }
    30453128
    3046                         if ( ! empty( $term ) && ! is_wp_error( $term ) )  {
    3047                                 $this->queried_object = $term;
    3048                                 $this->queried_object_id = (int) $term->term_id;
     3129                        if ( ! empty( $terms ) )  {
     3130                                if ( $this->is_category ) {
     3131                                        foreach ( $terms as &$category ) {
     3132                                                _make_cat_compat( $category );
     3133                                        }
     3134                                        unset( $category );
     3135                                }
    30493136
    3050                                 if ( $this->is_category )
    3051                                         _make_cat_compat( $this->queried_object );
     3137                                $this->queried_objects = $terms;
     3138                                $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'term_id' );
    30523139                        }
    30533140                } elseif ( $this->is_post_type_archive ) {
    3054                         $this->queried_object = get_post_type_object( $this->get('post_type') );
     3141                        $post_types = (array) $this->get( 'post_type' );
     3142                        foreach ( $post_types as $post_type ) {
     3143                                $this->queried_objects[] = get_post_type_object( $post_type );
     3144                        }
    30553145                } elseif ( $this->is_posts_page ) {
    3056                         $page_for_posts = get_option('page_for_posts');
    3057                         $this->queried_object = get_post( $page_for_posts );
    3058                         $this->queried_object_id = (int) $this->queried_object->ID;
    3059                 } elseif ( $this->is_singular && !is_null($this->post) ) {
    3060                         $this->queried_object = $this->post;
    3061                         $this->queried_object_id = (int) $this->post->ID;
     3146                        $page_for_posts = get_option( 'page_for_posts' );
     3147                        $this->queried_objects[] = get_post( $page_for_posts );
     3148                        $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'ID' );
     3149                } elseif ( $this->is_singular && ! is_null( $this->post ) ) {
     3150                        $this->queried_objects[] = $this->post;
     3151                        $this->queried_object_ids = wp_list_pluck( $this->queried_objects, 'ID' );
    30623152                } elseif ( $this->is_author ) {
    3063                         $this->queried_object_id = (int) $this->get('author');
    3064                         $this->queried_object = get_userdata( $this->queried_object_id );
     3153                        $this->queried_object_ids = array_map( 'absint', (array) $this->get( 'author' ) );
     3154                        foreach ( $this->queried_object_ids as $user_id ) {
     3155                                $this->queried_objects[] = get_userdata( $user_id );
     3156                        }
    30653157                }
    30663158
    3067                 return $this->queried_object;
     3159                // back-compat for get_queried_object and get_queried_object_id
     3160                $this->queried_object = ( ! empty( $this->queried_objects ) ) ? reset( $this->queried_objects ) : null;
     3161                $this->queried_object_id = ( ! empty( $this->queried_object_ids ) ) ? reset( $this->queried_object_ids ) : 0;
     3162
     3163                return $this->queried_objects;
    30683164        }
    30693165
    30703166        /**
    3071          * Retrieve ID of the current queried object.
     3167         * Retrieve IDs of the current queried objects.
    30723168         *
    3073          * @since 1.5.0
     3169         * @since 3.7.0
    30743170         * @access public
    30753171         *
    3076          * @return int
     3172         * @return array
    30773173         */
    3078         function get_queried_object_id() {
    3079                 $this->get_queried_object();
    3080 
    3081                 if ( isset($this->queried_object_id) ) {
    3082                         return $this->queried_object_id;
    3083                 }
    3084 
    3085                 return 0;
     3174        public function get_queried_object_ids() {
     3175                $this->get_queried_objects();
     3176                return $this->queried_object_ids;
    30863177        }
    30873178
    30883179        /**