Make WordPress Core

Ticket #7706: redir-to-real-post.patch

File redir-to-real-post.patch, 2.1 KB (added by azaozz, 18 years ago)
  • wp-includes/canonical.php

     
    3737 *              not needed or the string of the URL
    3838 */
    3939function redirect_canonical($requested_url=null, $do_redirect=true) {
    40         global $wp_rewrite, $is_IIS;
     40        global $wp_rewrite, $is_IIS, $wp_query, $wpdb;
    4141
    4242        if ( is_feed() || is_trackback() || is_search() || is_comments_popup() || is_admin() || $is_IIS || ( isset($_POST) && count($_POST) ) || is_preview() || is_robots() )
    4343                return;
     
    5959        $redirect = $original;
    6060        $redirect_url = false;
    6161
     62        if ( is_singular() && 1 > $wp_query->post_count && ($id = get_query_var('p')) ) {
     63
     64                $vars = $wpdb->get_results( $wpdb->prepare("SELECT post_type, post_parent FROM $wpdb->posts WHERE ID = %d", $id) );
     65
     66                if ( isset($vars[0]) && $vars = $vars[0] ) {
     67                        if ( 'revision' == $vars->post_type && $vars->post_parent > 0 )
     68                                $id = $vars->post_parent;
     69
     70                        if ( $redirect_url = get_permalink($id) )
     71                                $redirect['query'] = remove_query_arg(array('p', 'page_id', 'attachment_id'), $redirect['query']);
     72                }
     73        }
     74
    6275        // These tests give us a WP-generated permalink
    6376        if ( is_404() ) {
    6477                $redirect_url = redirect_guess_404_permalink();
    6578        } elseif ( is_object($wp_rewrite) && $wp_rewrite->using_permalinks() ) {
    6679                // rewriting of old ?p=X, ?m=2004, ?m=200401, ?m=20040101
    67                 if ( is_single() && isset($_GET['p']) ) {
     80                if ( is_single() && isset($_GET['p']) && ! $redirect_url ) {
    6881                        if ( $redirect_url = get_permalink(get_query_var('p')) )
    6982                                $redirect['query'] = remove_query_arg('p', $redirect['query']);
    70                 } elseif ( is_page() && isset($_GET['page_id']) ) {
     83                } elseif ( is_page() && isset($_GET['page_id']) && ! $redirect_url ) {
    7184                        if ( $redirect_url = get_permalink(get_query_var('page_id')) )
    7285                                $redirect['query'] = remove_query_arg('page_id', $redirect['query']);
    7386                } elseif ( isset($_GET['m']) && ( is_year() || is_month() || is_day() ) ) {
     
    253266 */
    254267function redirect_guess_404_permalink() {
    255268        global $wpdb;
     269
    256270        if ( !get_query_var('name') )
    257271                return false;
    258272