WordPress.org

Make WordPress Core

Ticket #7022: 7022.2.diff

File 7022.2.diff, 2.1 KB (added by DD32, 7 years ago)

oops, Should've included updated PHPDoc for that diff

  • wp-includes/canonical.php

     
    216216 * @since 2.3 
    217217 * @uses $wpdb 
    218218 * 
     219 * @param bool $refine Optional. Look for a partial match & redine it to only in the date range given. 
    219220 * @return bool|string Returns False, if it can't find post, returns correct location on success. 
    220221 */ 
    221 function redirect_guess_404_permalink() { 
     222function redirect_guess_404_permalink($refine = true) { 
    222223        global $wpdb; 
    223224        if ( !get_query_var('name') ) 
    224225                return false; 
    225226 
    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  
     227        if( ! $refine ){ 
     228                $where = $wpdb->prepare("post_name = %s", get_query_var('name')); //Want a exact match as its not a refined query 
     229        } else { 
     230                $where = $wpdb->prepare("post_name LIKE %s", get_query_var('name') . '%'); 
     231                // if any of year, monthnum, or day are set, use them to refine the query 
     232                if ( get_query_var('year') ) 
     233                        $where .= $wpdb->prepare(" AND YEAR(post_date) = %d", get_query_var('year')); 
     234                if ( get_query_var('monthnum') ) 
     235                        $where .= $wpdb->prepare(" AND MONTH(post_date) = %d", get_query_var('monthnum')); 
     236                if ( get_query_var('day') ) 
     237                        $where .= $wpdb->prepare(" AND DAYOFMONTH(post_date) = %d", get_query_var('day')); 
     238        } 
    236239        $post_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE $where AND post_status = 'publish'"); 
    237         if ( !$post_id ) 
    238                 return false; 
     240        if ( !$post_id ){ 
     241                if( $refine ) 
     242                        return redirect_guess_404_permalink(false); //Look for a non-refined match. 
     243                else 
     244                        return false; //It has allready looked for a non-refined match 
     245        } 
    239246        return get_permalink($post_id); 
    240247} 
    241248