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
This commit is contained in:
Peter Wilson 2023-02-03 03:58:16 +00:00
parent 7bc792bb73
commit 592be344b4
3 changed files with 62 additions and 60 deletions

View File

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

View File

@ -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.
*

View File

@ -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.