Created
November 5, 2012 23:04
-
-
Save tlongren/4021002 to your computer and use it in GitHub Desktop.
Possible Slow Query Fix
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter('pre_get_posts', 'optimized_get_posts', 100); | |
function optimized_get_posts() { | |
global $wp_query, $wpdb; | |
$wp_query->query_vars['no_found_rows'] = 1; | |
$wp_query->found_posts = $wpdb->get_var( "SELECT COUNT(*) FROM wp_posts WHERE 1=1 AND wp_posts.post_type = 'post' AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')" ); | |
$wp_query->found_posts = apply_filters_ref_array( 'found_posts', array( $wp_query->found_posts, &$wp_query ) ); | |
$wp_query->max_num_pages = ceil($wp_query->found_posts / $wp_query->query_vars['posts_per_page']); | |
return $wp_query; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does this work well with newer versions of WP such as 4.2.3 or 4.3?