From 59ff3d2bb198b9e1210239a3a50afe9e464869b4 Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Thu, 10 Nov 2022 02:21:15 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/branches/6.1@54783 git-svn-id: http://core.svn.wordpress.org/branches/6.1@54335 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/post.php | 49 +++++++++++++++++++++++++++-------------- wp-includes/version.php | 2 +- 2 files changed, 34 insertions(+), 17 deletions(-) diff --git a/wp-includes/post.php b/wp-includes/post.php index d7e7bfec99..305d8f9551 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5766,6 +5766,8 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { * @since 2.1.0 * @since 3.0.0 The `$post_type` parameter was added. * + * @global wpdb $wpdb WordPress database abstraction object. + * * @param string $page_title Page title. * @param string $output Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which * correspond to a WP_Post object, an associative array, or a numeric array, @@ -5774,25 +5776,40 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { * @return WP_Post|array|null WP_Post (or array) on success, or null on failure. */ function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) { - $args = array( - 'title' => $page_title, - 'post_type' => $post_type, - 'post_status' => get_post_stati(), - 'posts_per_page' => 1, - 'update_post_term_cache' => false, - 'update_post_meta_cache' => false, - 'no_found_rows' => true, - 'orderby' => 'post_date ID', - 'order' => 'ASC', - ); - $query = new WP_Query( $args ); - $pages = $query->posts; + global $wpdb; - if ( empty( $pages ) ) { - return null; + if ( is_array( $post_type ) ) { + $post_type = esc_sql( $post_type ); + $post_type_in_string = "'" . implode( "','", $post_type ) . "'"; + $sql = $wpdb->prepare( + " + SELECT ID + FROM $wpdb->posts + WHERE post_title = %s + AND post_type IN ($post_type_in_string) + ", + $page_title + ); + } else { + $sql = $wpdb->prepare( + " + SELECT ID + FROM $wpdb->posts + WHERE post_title = %s + AND post_type = %s + ", + $page_title, + $post_type + ); } - return get_post( $pages[0], $output ); + $page = $wpdb->get_var( $sql ); + + if ( $page ) { + return get_post( $page, $output ); + } + + return null; } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index cc11c9237e..7376268a8b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1.1-alpha-54781'; +$wp_version = '6.1.1-alpha-54783'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.