Make WordPress Core

Changeset 55215


Ignore:
Timestamp:
02/03/2023 04:15:55 PM (16 months ago)
Author:
spacedmonkey
Message:

Query: Revert [55169].

Revert [55169] and mark #56689 as wontfix. get_page_by_path can not use WP_Query, as is results in fatel errors for those using get_page_by_path in the pre_get_posts action or posts_pre_query filter. This sadly means that WP_Query can never be used in get_page_by_path.

Props spacedmonkey, iandunn, mukesh27, joemcgill, adamsilverstein, otto42.
See #56689.

File:
1 edited

Legend:

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

    r55207 r55215  
    56765676 * @since 2.1.0
    56775677 *
     5678 * @global wpdb $wpdb WordPress database abstraction object.
     5679 *
    56785680 * @param string       $page_path Page path.
    56795681 * @param string       $output    Optional. The required return type. One of OBJECT, ARRAY_A, or ARRAY_N, which
     
    56845686 */
    56855687function get_page_by_path( $page_path, $output = OBJECT, $post_type = 'page' ) {
    5686     $page_path = rawurlencode( urldecode( $page_path ) );
    5687     $page_path = str_replace( '%2F', '/', $page_path );
    5688     $page_path = str_replace( '%20', ' ', $page_path );
    5689     $parts     = explode( '/', trim( $page_path, '/' ) );
    5690     $parts     = array_map( 'sanitize_title_for_query', $parts );
     5688    global $wpdb;
     5689
     5690    $last_changed = wp_cache_get_last_changed( 'posts' );
     5691
     5692    $hash      = md5( $page_path . serialize( $post_type ) );
     5693    $cache_key = "get_page_by_path:$hash:$last_changed";
     5694    $cached    = wp_cache_get( $cache_key, 'posts' );
     5695    if ( false !== $cached ) {
     5696        // Special case: '0' is a bad `$page_path`.
     5697        if ( '0' === $cached || 0 === $cached ) {
     5698            return;
     5699        } else {
     5700            return get_post( $cached, $output );
     5701        }
     5702    }
     5703
     5704    $page_path     = rawurlencode( urldecode( $page_path ) );
     5705    $page_path     = str_replace( '%2F', '/', $page_path );
     5706    $page_path     = str_replace( '%20', ' ', $page_path );
     5707    $parts         = explode( '/', trim( $page_path, '/' ) );
     5708    $parts         = array_map( 'sanitize_title_for_query', $parts );
     5709    $escaped_parts = esc_sql( $parts );
     5710
     5711    $in_string = "'" . implode( "','", $escaped_parts ) . "'";
    56915712
    56925713    if ( is_array( $post_type ) ) {
     
    56965717    }
    56975718
    5698     $args = array(
    5699         'post_name__in'          => $parts,
    5700         'post_type'              => $post_types,
    5701         'post_status'            => 'all',
    5702         'posts_per_page'         => -1,
    5703         'update_post_term_cache' => false,
    5704         'update_post_meta_cache' => false,
    5705         'no_found_rows'          => true,
    5706         'orderby'                => 'none',
    5707     );
    5708 
    5709     $query = new WP_Query( $args );
    5710     $posts = $query->get_posts();
    5711     $pages = array();
    5712 
    5713     foreach ( $posts as $post ) {
    5714         $pages[ $post->ID ] = $post;
    5715     }
     5719    $post_types          = esc_sql( $post_types );
     5720    $post_type_in_string = "'" . implode( "','", $post_types ) . "'";
     5721    $sql                 = "
     5722        SELECT ID, post_name, post_parent, post_type
     5723        FROM $wpdb->posts
     5724        WHERE post_name IN ($in_string)
     5725        AND post_type IN ($post_type_in_string)
     5726    ";
     5727
     5728    $pages = $wpdb->get_results( $sql, OBJECT_K );
    57165729
    57175730    $revparts = array_reverse( $parts );
    57185731
    57195732    $foundid = 0;
    5720     foreach ( $pages as $page ) {
     5733    foreach ( (array) $pages as $page ) {
    57215734        if ( $page->post_name == $revparts[0] ) {
    57225735            $count = 0;
     
    57445757        }
    57455758    }
     5759
     5760    // We cache misses as well as hits.
     5761    wp_cache_set( $cache_key, $foundid, 'posts' );
    57465762
    57475763    if ( $foundid ) {
Note: See TracChangeset for help on using the changeset viewer.