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.
Fixes #57039, #56991.
See #57041.


Built from https://develop.svn.wordpress.org/trunk@54782


git-svn-id: http://core.svn.wordpress.org/trunk@54334 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-11-10 00:46:16 +00:00
parent 735313df84
commit 0f7fcc2863
2 changed files with 34 additions and 17 deletions

View File

@ -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;
}
/**

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54777';
$wp_version = '6.2-alpha-54782';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.