Make WordPress Core

Changeset 54783 for branches/6.1


Ignore:
Timestamp:
11/10/2022 02:19:05 AM (4 years ago)
Author:
peterwilsoncc
Message:

Posts, Post Types: Revert get_page_by_title()'s use of WP_Query.

Revert to legacy database query in get_pages_by_title(). Due to the lack of orderby clause in the previous database query, it is not possible to gain consistent results by converting the function to a WP_Query wrapper.

Reverts [54271, 54242, 54234].

Props Bjorn2404, 10upsimon, dilipbheda, mukesh27, spacedmonkey, TimothyBlynJacobs, rjasdfiii, stentibbing, pbiron, pento.
Merges [54782] to the 6.1 branch.
Fixes #57039, #56991.
See #57041.

Location:
branches/6.1
Files:
1 deleted
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

  • branches/6.1/src/wp-includes/post.php

    r54713 r54783  
    57675767 * @since 3.0.0 The `$post_type` parameter was added.
    57685768 *
     5769 * @global wpdb $wpdb WordPress database abstraction object.
     5770 *
    57695771 * @param string       $page_title Page title.
    57705772 * @param string       $output     Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
     
    57755777 */
    57765778function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    5777         $args  = array(
    5778                 'title'                  => $page_title,
    5779                 'post_type'              => $post_type,
    5780                 'post_status'            => get_post_stati(),
    5781                 'posts_per_page'         => 1,
    5782                 'update_post_term_cache' => false,
    5783                 'update_post_meta_cache' => false,
    5784                 'no_found_rows'          => true,
    5785                 'orderby'                => 'post_date ID',
    5786                 'order'                  => 'ASC',
    5787         );
    5788         $query = new WP_Query( $args );
    5789         $pages = $query->posts;
    5790 
    5791         if ( empty( $pages ) ) {
    5792                 return null;
    5793         }
    5794 
    5795         return get_post( $pages[0], $output );
     5779        global $wpdb;
     5780
     5781        if ( is_array( $post_type ) ) {
     5782                $post_type           = esc_sql( $post_type );
     5783                $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
     5784                $sql                 = $wpdb->prepare(
     5785                        "
     5786                        SELECT ID
     5787                        FROM $wpdb->posts
     5788                        WHERE post_title = %s
     5789                        AND post_type IN ($post_type_in_string)
     5790                ",
     5791                        $page_title
     5792                );
     5793        } else {
     5794                $sql = $wpdb->prepare(
     5795                        "
     5796                        SELECT ID
     5797                        FROM $wpdb->posts
     5798                        WHERE post_title = %s
     5799                        AND post_type = %s
     5800                ",
     5801                        $page_title,
     5802                        $post_type
     5803                );
     5804        }
     5805
     5806        $page = $wpdb->get_var( $sql );
     5807
     5808        if ( $page ) {
     5809                return get_post( $page, $output );
     5810        }
     5811
     5812        return null;
    57965813}
    57975814
Note: See TracChangeset for help on using the changeset viewer.