Make WordPress Core

Ticket #47068: 47068.diff

File 47068.diff, 7.5 KB (added by iflairwebtechnologies, 7 months ago)
  • src/wp-includes/class-wp-query.php

     
    39703970         * @return WP_Term|WP_Post_Type|WP_Post|WP_User|null The queried object.
    39713971         */
    39723972        public function get_queried_object() {
    3973                 if ( isset( $this->queried_object ) ) {
    3974                         return $this->queried_object;
    3975                 }
     3973            if ( isset( $this->queried_object ) ) {
     3974                return $this->queried_object;
     3975            }
    39763976
    3977                 $this->queried_object    = null;
    3978                 $this->queried_object_id = null;
     3977            $this->queried_object    = null;
     3978            $this->queried_object_id = null;
    39793979
    3980                 if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    3981                         if ( $this->is_category ) {
    3982                                 $cat           = $this->get( 'cat' );
    3983                                 $category_name = $this->get( 'category_name' );
     3980            if ( $this->is_category || $this->is_tag || $this->is_tax ) {
    39843981
    3985                                 if ( $cat ) {
    3986                                         $term = get_term( $cat, 'category' );
    3987                                 } elseif ( $category_name ) {
    3988                                         $term = get_term_by( 'slug', $category_name, 'category' );
    3989                                 }
    3990                         } elseif ( $this->is_tag ) {
    3991                                 $tag_id = $this->get( 'tag_id' );
    3992                                 $tag    = $this->get( 'tag' );
     3982                if ( $this->is_category ) {
     3983                    $cat           = $this->get( 'cat' );
     3984                    $category_name = $this->get( 'category_name' );
    39933985
    3994                                 if ( $tag_id ) {
    3995                                         $term = get_term( $tag_id, 'post_tag' );
    3996                                 } elseif ( $tag ) {
    3997                                         $term = get_term_by( 'slug', $tag, 'post_tag' );
    3998                                 }
    3999                         } else {
    4000                                 // For other tax queries, grab the first term from the first clause.
    4001                                 if ( ! empty( $this->tax_query->queried_terms ) ) {
    4002                                         $queried_taxonomies = array_keys( $this->tax_query->queried_terms );
    4003                                         $matched_taxonomy   = reset( $queried_taxonomies );
    4004                                         $query              = $this->tax_query->queried_terms[ $matched_taxonomy ];
     3986                    if ( $cat ) {
     3987                        $term = get_term( $cat, 'category' );
     3988                    } elseif ( $category_name ) {
     3989                        $term = get_term_by( 'slug', $category_name, 'category' );
     3990                    }
    40053991
    4006                                         if ( ! empty( $query['terms'] ) ) {
    4007                                                 if ( 'term_id' === $query['field'] ) {
    4008                                                         $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
    4009                                                 } else {
    4010                                                         $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
    4011                                                 }
    4012                                         }
    4013                                 }
    4014                         }
     3992                } elseif ( $this->is_tag ) {
     3993                    $tag_id = $this->get( 'tag_id' );
     3994                    $tag    = $this->get( 'tag' );
    40153995
    4016                         if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
    4017                                 $this->queried_object    = $term;
    4018                                 $this->queried_object_id = (int) $term->term_id;
     3996                    if ( $tag_id ) {
     3997                        $term = get_term( $tag_id, 'post_tag' );
     3998                    } elseif ( $tag ) {
     3999                        $term = get_term_by( 'slug', $tag, 'post_tag' );
     4000                    }
    40194001
    4020                                 if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) {
    4021                                         _make_cat_compat( $this->queried_object );
    4022                                 }
    4023                         }
    4024                 } elseif ( $this->is_post_type_archive ) {
    4025                         $post_type = $this->get( 'post_type' );
     4002                } else {
     4003                    // For other tax queries, grab the first term from the first clause.
     4004                    if ( ! empty( $this->tax_query->queried_terms ) ) {
     4005                        $queried_taxonomies = array_keys( $this->tax_query->queried_terms );
     4006                        $matched_taxonomy   = reset( $queried_taxonomies );
     4007                        $query              = $this->tax_query->queried_terms[ $matched_taxonomy ];
    40264008
    4027                         if ( is_array( $post_type ) ) {
    4028                                 $post_type = reset( $post_type );
    4029                         }
     4009                        if ( ! empty( $query['terms'] ) ) {
     4010                            if ( 'term_id' === $query['field'] ) {
     4011                                $term = get_term( reset( $query['terms'] ), $matched_taxonomy );
     4012                            } else {
     4013                                $term = get_term_by( $query['field'], reset( $query['terms'] ), $matched_taxonomy );
     4014                            }
     4015                        }
     4016                    }
     4017                }
    40304018
    4031                         $this->queried_object = get_post_type_object( $post_type );
    4032                 } elseif ( $this->is_posts_page ) {
    4033                         $page_for_posts = get_option( 'page_for_posts' );
     4019                if ( ! empty( $term ) && ! is_wp_error( $term ) ) {
     4020                    $this->queried_object    = $term;
     4021                    $this->queried_object_id = (int) $term->term_id;
    40344022
    4035                         $this->queried_object    = get_post( $page_for_posts );
    4036                         $this->queried_object_id = (int) $this->queried_object->ID;
    4037                 } elseif ( $this->is_singular && ! empty( $this->post ) ) {
    4038                         $this->queried_object    = $this->post;
    4039                         $this->queried_object_id = (int) $this->post->ID;
    4040                 } elseif ( $this->is_author ) {
    4041                         $author      = (int) $this->get( 'author' );
    4042                         $author_name = $this->get( 'author_name' );
     4023                    if ( $this->is_category && 'category' === $this->queried_object->taxonomy ) {
     4024                        _make_cat_compat( $this->queried_object );
     4025                    }
     4026                }
    40434027
    4044                         if ( $author ) {
    4045                                 $this->queried_object_id = $author;
    4046                         } elseif ( $author_name ) {
    4047                                 $user = get_user_by( 'slug', $author_name );
     4028            } elseif ( $this->is_post_type_archive ) {
    40484029
    4049                                 if ( $user ) {
    4050                                         $this->queried_object_id = $user->ID;
    4051                                 }
    4052                         }
     4030                $post_type = $this->get( 'post_type' );
     4031                if ( is_array( $post_type ) ) {
     4032                    $post_type = reset( $post_type );
     4033                }
    40534034
    4054                         $this->queried_object = get_userdata( $this->queried_object_id );
    4055                 }
     4035                $this->queried_object = get_post_type_object( $post_type );
    40564036
    4057                 return $this->queried_object;
     4037            } elseif ( $this->is_posts_page ) {
     4038
     4039                $page_for_posts = get_option( 'page_for_posts' );
     4040
     4041                $this->queried_object    = get_post( $page_for_posts );
     4042                $this->queried_object_id = (int) $this->queried_object->ID;
     4043
     4044            } elseif ( $this->is_singular && ! empty( $this->post ) ) {
     4045
     4046                $this->queried_object    = $this->post;
     4047                $this->queried_object_id = (int) $this->post->ID;
     4048
     4049            } elseif ( $this->is_author ) {
     4050
     4051                $author      = (int) $this->get( 'author' );
     4052                $author_name = $this->get( 'author_name' );
     4053
     4054                if ( $author ) {
     4055                    $this->queried_object_id = $author;
     4056                } elseif ( $author_name ) {
     4057                    $user = get_user_by( 'slug', $author_name );
     4058                    if ( $user ) {
     4059                        $this->queried_object_id = $user->ID;
     4060                    }
     4061                }
     4062
     4063                $this->queried_object = get_userdata( $this->queried_object_id );
     4064            }
     4065
     4066            /**
     4067             * ----------------------------------------------------------
     4068             *  FIX 1: Homepage = "Your latest posts" → return post object
     4069             * ----------------------------------------------------------
     4070             */
     4071            if ( $this->is_home && ! $this->is_posts_page && null === $this->queried_object ) {
     4072                $this->queried_object = get_post_type_object( 'post' );
     4073                return $this->queried_object;
     4074            }
     4075
     4076            /**
     4077             * ----------------------------------------------------------
     4078             * FIX 2: Date archives → return date object instead of null
     4079             * ----------------------------------------------------------
     4080             */
     4081            if ( $this->is_date && null === $this->queried_object ) {
     4082                $date_obj = new stdClass();
     4083                $date_obj->year     = get_query_var( 'year' );
     4084                $date_obj->monthnum = get_query_var( 'monthnum' );
     4085                $date_obj->day      = get_query_var( 'day' );
     4086
     4087                $this->queried_object = $date_obj;
     4088                return $this->queried_object;
     4089            }
     4090
     4091            return $this->queried_object;
    40584092        }
    40594093
    40604094        /**