WordPress.org

Make WordPress Core

Ticket #18120: get_adjacent_post.diff

File get_adjacent_post.diff, 1.1 KB (added by apokalyptik, 2 years ago)

Select p.id and then get_post(). Avoids temp table *and* filesort which occurs on large posts tables where cats are (at least) excluded)

  • wp-includes/link-template.php

     
    11481148        $where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories ); 
    11491149        $sort  = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1" ); 
    11501150 
    1151         $query = "SELECT p.* FROM $wpdb->posts AS p $join $where $sort"; 
     1151        $query = "SELECT p.id FROM $wpdb->posts AS p $join $where $sort"; 
    11521152        $query_key = 'adjacent_post_' . md5($query); 
    11531153        $result = wp_cache_get($query_key, 'counts'); 
    11541154        if ( false !== $result ) 
    11551155                return $result; 
    11561156 
    1157         $result = $wpdb->get_row("SELECT p.* FROM $wpdb->posts AS p $join $where $sort"); 
     1157        $result = $wpdb->get_var("SELECT p.id FROM $wpdb->posts AS p $join $where $sort"); 
    11581158        if ( null === $result ) 
    11591159                $result = ''; 
     1160         
     1161        $result = get_post( $result ); 
    11601162 
    11611163        wp_cache_set($query_key, $result, 'counts'); 
    11621164        return $result;