Make WordPress Core


Ignore:
Timestamp:
02/03/2023 03:56:10 AM (4 years ago)
Author:
peterwilsoncc
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/deprecated.php

    r54997 r55207  
    45294529        return $clauses;
    45304530}
     4531
     4532/**
     4533 * Retrieves a page given its title.
     4534 *
     4535 * If more than one post uses the same title, the post with the smallest ID will be returned.
     4536 * Be careful: in case of more than one post having the same title, it will check the oldest
     4537 * publication date, not the smallest ID.
     4538 *
     4539 * Because this function uses the MySQL '=' comparison, $page_title will usually be matched
     4540 * as case-insensitive with default collation.
     4541 *
     4542 * @since 2.1.0
     4543 * @since 3.0.0 The `$post_type` parameter was added.
     4544 * @deprecated 6.2.0 Use WP_Query.
     4545 *
     4546 * @global wpdb $wpdb WordPress database abstraction object.
     4547 *
     4548 * @param string       $page_title Page title.
     4549 * @param string       $output     Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
     4550 *                                 correspond to a WP_Post object, an associative array, or a numeric array,
     4551 *                                 respectively. Default OBJECT.
     4552 * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
     4553 * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
     4554 */
     4555function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
     4556        _deprecated_function( __FUNCTION__, '6.2.0', 'WP_Query' );
     4557        global $wpdb;
     4558
     4559        if ( is_array( $post_type ) ) {
     4560                $post_type           = esc_sql( $post_type );
     4561                $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
     4562                $sql                 = $wpdb->prepare(
     4563                        "
     4564                        SELECT ID
     4565                        FROM $wpdb->posts
     4566                        WHERE post_title = %s
     4567                        AND post_type IN ($post_type_in_string)
     4568                ",
     4569                        $page_title
     4570                );
     4571        } else {
     4572                $sql = $wpdb->prepare(
     4573                        "
     4574                        SELECT ID
     4575                        FROM $wpdb->posts
     4576                        WHERE post_title = %s
     4577                        AND post_type = %s
     4578                ",
     4579                        $page_title,
     4580                        $post_type
     4581                );
     4582        }
     4583
     4584        $page = $wpdb->get_var( $sql );
     4585
     4586        if ( $page ) {
     4587                return get_post( $page, $output );
     4588        }
     4589
     4590        return null;
     4591}
Note: See TracChangeset for help on using the changeset viewer.