Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Possible Slow Query Fix
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;
}
@bigmike7801

This comment has been minimized.

Copy link

@bigmike7801 bigmike7801 commented Sep 5, 2015

Does this work well with newer versions of WP such as 4.2.3 or 4.3?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment