From 592be344b4b4e25e2dd2b42628d27576635b86af Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Fri, 3 Feb 2023 03:58:16 +0000 Subject: [PATCH] Posts, Post Types: Deprecate `get_page_by_title()` in favour of `WP_Query`. Formally deprecate `get_page_by_title()`. In its current form the function is unpredictable in that it may return a result that leads to a 404 error and will return different results depending on the database version/engine combination used. It is recommended developers use `WP_Query` instead: {{{ $query = new WP_Query( array( 'post_type' => 'page', 'title' => 'Sample Page', ) ); }}} Props TimothyBlynJacobs, costdev, mukesh27, spacedmonkey, peterwilsoncc. Fixes #57041. Built from https://develop.svn.wordpress.org/trunk@55207 git-svn-id: http://core.svn.wordpress.org/trunk@54740 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/deprecated.php | 61 ++++++++++++++++++++++++++++++++++++++ wp-includes/post.php | 59 ------------------------------------ wp-includes/version.php | 2 +- 3 files changed, 62 insertions(+), 60 deletions(-) diff --git a/wp-includes/deprecated.php b/wp-includes/deprecated.php index 62ff08b534..3787d71297 100644 --- a/wp-includes/deprecated.php +++ b/wp-includes/deprecated.php @@ -4528,3 +4528,64 @@ function _filter_query_attachment_filenames( $clauses ) { remove_filter( 'posts_clauses', __FUNCTION__ ); return $clauses; } + +/** + * Retrieves a page given its title. + * + * If more than one post uses the same title, the post with the smallest ID will be returned. + * Be careful: in case of more than one post having the same title, it will check the oldest + * publication date, not the smallest ID. + * + * Because this function uses the MySQL '=' comparison, $page_title will usually be matched + * as case-insensitive with default collation. + * + * @since 2.1.0 + * @since 3.0.0 The `$post_type` parameter was added. + * @deprecated 6.2.0 Use WP_Query. + * + * @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, + * respectively. Default OBJECT. + * @param string|array $post_type Optional. Post type or array of post types. Default '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' ) { + _deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' ); + global $wpdb; + + 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 + ); + } + + $page = $wpdb->get_var( $sql ); + + if ( $page ) { + return get_post( $page, $output ); + } + + return null; +} diff --git a/wp-includes/post.php b/wp-includes/post.php index 8647c7f0f9..fb85d8bfd9 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -5751,65 +5751,6 @@ function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) { return null; } -/** - * Retrieves a page given its title. - * - * If more than one post uses the same title, the post with the smallest ID will be returned. - * Be careful: in case of more than one post having the same title, it will check the oldest - * publication date, not the smallest ID. - * - * Because this function uses the MySQL '=' comparison, $page_title will usually be matched - * as case-insensitive with default collation. - * - * @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, - * respectively. Default OBJECT. - * @param string|array $post_type Optional. Post type or array of post types. Default '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' ) { - global $wpdb; - - 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 - ); - } - - $page = $wpdb->get_var( $sql ); - - if ( $page ) { - return get_post( $page, $output ); - } - - return null; -} - /** * Identifies descendants of a given page ID in a list of page objects. * diff --git a/wp-includes/version.php b/wp-includes/version.php index 7ea53c6f6a..914b9ae650 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.2-alpha-55206'; +$wp_version = '6.2-alpha-55207'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.