Make WordPress Core

Changeset 55207


Ignore:
Timestamp:
02/03/2023 03:56:10 AM (2 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.

Location:
trunk
Files:
1 added
2 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}
  • trunk/src/wp-includes/post.php

    r55202 r55207  
    57535753
    57545754/**
    5755  * Retrieves a page given its title.
    5756  *
    5757  * If more than one post uses the same title, the post with the smallest ID will be returned.
    5758  * Be careful: in case of more than one post having the same title, it will check the oldest
    5759  * publication date, not the smallest ID.
    5760  *
    5761  * Because this function uses the MySQL '=' comparison, $page_title will usually be matched
    5762  * as case-insensitive with default collation.
    5763  *
    5764  * @since 2.1.0
    5765  * @since 3.0.0 The `$post_type` parameter was added.
    5766  *
    5767  * @global wpdb $wpdb WordPress database abstraction object.
    5768  *
    5769  * @param string       $page_title Page title.
    5770  * @param string       $output     Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
    5771  *                                 correspond to a WP_Post object, an associative array, or a numeric array,
    5772  *                                 respectively. Default OBJECT.
    5773  * @param string|array $post_type  Optional. Post type or array of post types. Default 'page'.
    5774  * @return WP_Post|array|null WP_Post (or array) on success, or null on failure.
    5775  */
    5776 function get_page_by_title( $page_title, $output = OBJECT, $post_type = 'page' ) {
    5777     global $wpdb;
    5778 
    5779     if ( is_array( $post_type ) ) {
    5780         $post_type           = esc_sql( $post_type );
    5781         $post_type_in_string = "'" . implode( "','", $post_type ) . "'";
    5782         $sql                 = $wpdb->prepare(
    5783             "
    5784             SELECT ID
    5785             FROM $wpdb->posts
    5786             WHERE post_title = %s
    5787             AND post_type IN ($post_type_in_string)
    5788         ",
    5789             $page_title
    5790         );
    5791     } else {
    5792         $sql = $wpdb->prepare(
    5793             "
    5794             SELECT ID
    5795             FROM $wpdb->posts
    5796             WHERE post_title = %s
    5797             AND post_type = %s
    5798         ",
    5799             $page_title,
    5800             $post_type
    5801         );
    5802     }
    5803 
    5804     $page = $wpdb->get_var( $sql );
    5805 
    5806     if ( $page ) {
    5807         return get_post( $page, $output );
    5808     }
    5809 
    5810     return null;
    5811 }
    5812 
    5813 /**
    58145755 * Identifies descendants of a given page ID in a list of page objects.
    58155756 *
Note: See TracChangeset for help on using the changeset viewer.