WordPress.org

Make WordPress Core

Ticket #3451: 3451.canonical.redirect-pages.diff

File 3451.canonical.redirect-pages.diff, 2.2 KB (added by DD32, 10 years ago)
  • wp-includes/canonical.php

     
    2626
    2727        // These tests give us a WP-generated permalink
    2828        if ( is_404() ) {
    29                 $redirect_url = redirect_guess_404_permalink();
     29                $redirect_url = redirect_guess_404_permalink($original);
    3030        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
    3131                // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
    3232                if ( is_single() && isset($_GET['p']) ) {
     
    157157        }
    158158
    159159        if ( $redirect_url && $redirect_url != $requested_url ) {
    160                 // var_dump($redirect_url); die();
    161160                $redirect_url = apply_filters('redirect_canonical', $redirect_url, $requested_url);
    162161                if ( $do_redirect) {
    163162                        // protect against chained redirects
     
    175174        }
    176175}
    177176
    178 function redirect_guess_404_permalink() {
     177function redirect_guess_404_permalink($original_url) {
    179178        global $wpdb;
    180         if ( !get_query_var('name') )
    181                 return false;
     179        $name = get_query_var('name');
     180        $order = '';
    182181
    183         $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%');
     182        if ( empty($name) ){
     183                // Check to see if we can pull a postname/page name from the url thats not in the permalink structure
     184                //Match string that starts with /, does not contain / and optionally may be followed by / then end of string
     185                if( ! preg_match('|\/([^\/.]+)\/?$|', $original_url['path'], $pagemat ) )
     186                        return false;
     187                $name = $pagemat[1];
     188                $order = "ORDER BY post_parent ASC, post_name ASC";
     189        }
    184190
     191        $where = $wpdb->prepare("post_name LIKE %s", $name . '%');
     192
    185193        // if any of year, monthnum, or day are set, use them to refine the query
    186194        if ( get_query_var('year') )
    187195                $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year'));
     
    190198        if ( get_query_var('day') )
    191199                $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day'));
    192200
    193         $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'");
     201        $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish' $order LIMIT 1");
    194202        if ( !$post_id )
    195203                return false;
    196204        return get_permalink($post_id);