WordPress.org

Make WordPress Core

Ticket #7022: 7022.diff

File 7022.diff, 2.0 KB (added by DD32, 6 years ago)
  • wp-includes/canonical.php

     
    218218 * 
    219219 * @return bool|string Returns False, if it can't find post, returns correct location on success. 
    220220 */ 
    221 function redirect_guess_404_permalink() { 
     221function redirect_guess_404_permalink($refine = true) { 
    222222        global $wpdb; 
    223223        if ( !get_query_var('name') ) 
    224224                return false; 
    225225 
    226         $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); 
    227  
    228         // if any of year, monthnum, or day are set, use them to refine the query 
    229         if ( get_query_var('year') ) 
    230                 $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); 
    231         if ( get_query_var('monthnum') ) 
    232                 $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); 
    233         if ( get_query_var('day') ) 
    234                 $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); 
    235  
     226        if( ! $refine ){ 
     227                $where = $wpdb->prepare("post_name = %s", get_query_var('name')); //Want a exact match as its not a refined query 
     228        } else { 
     229                $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); 
     230                // if any of year, monthnum, or day are set, use them to refine the query 
     231                if ( get_query_var('year') ) 
     232                        $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); 
     233                if ( get_query_var('monthnum') ) 
     234                        $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); 
     235                if ( get_query_var('day') ) 
     236                        $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); 
     237        } 
    236238        $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); 
    237         if ( !$post_id ) 
    238                 return false; 
     239        if ( !$post_id ){ 
     240                if( $refine ) 
     241                        return redirect_guess_404_permalink(false); //Look for a non-refined match. 
     242                else 
     243                        return false; //It has allready looked for a non-refined match 
     244        } 
    239245        return get_permalink($post_id); 
    240246} 
    241247