Make WordPress Core


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