Wordpress SQL_CALC fix causes PHP error

Posted by ok1ha on Stack Overflow See other posts from Stack Overflow or by ok1ha
Published on 2012-04-10T05:25:26Z Indexed on 2012/04/10 5:28 UTC
Read the original article Hit count: 162

Filed under:
|
|

I'm looking for some followup on an older topic for Wordpress where SQL_CALC was found to slow things down when you have a large DB in Wordpress. I have been using the code, at the bottom of this post, to get around it but it does generate an error in my error log. How would I prevent this error?

 PHP Warning:  Division by zero in /var/www/vhosts/domain.com/httpdocs/wp-content/themes/greatTheme/functions.php on line 19

The original thread: http://wordpress.org/support/topic/slow-queries-sql_calc_found_rows-bringing-down-site?replies=25

The code in my functions.php:

 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;
 }

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql