[2.3] Replace max_related_posts_query with wp_count_posts

This commit is contained in:
Mike Jolley 2015-05-25 01:20:31 +01:00
parent fe8dc44ff6
commit 1b73322874
1 changed files with 2 additions and 11 deletions

View File

@ -1509,18 +1509,9 @@ class WC_Product {
$query = apply_filters( 'woocommerce_product_related_posts_query', $query, $this->id ); $query = apply_filters( 'woocommerce_product_related_posts_query', $query, $this->id );
// How many rows total?
$max_related_posts_transient_name = 'wc_max_related_' . $this->id . WC_Cache_Helper::get_transient_version( 'product' );
if ( false === ( $max_related_posts = get_transient( $max_related_posts_transient_name ) ) ) {
$max_related_posts_query = $query;
$max_related_posts_query['fields'] = "SELECT COUNT(DISTINCT ID) FROM {$wpdb->posts} p";
$max_related_posts = absint( $wpdb->get_var( implode( ' ', apply_filters( 'woocommerce_product_max_related_posts_query', $max_related_posts_query, $this->id ) ) ) );
set_transient( $max_related_posts_transient_name, $max_related_posts, DAY_IN_SECONDS * 30 );
}
// Generate limit // Generate limit
$offset = $max_related_posts < $limit ? 0 : absint( rand( 0, $max_related_posts - $limit ) ); $product_count = wp_count_posts( 'product' );
$offset = $product_count->publish < $limit ? 0 : absint( rand( 0, $product_count->publish - $limit ) );
$query['limits'] = " LIMIT {$offset}, {$limit} "; $query['limits'] = " LIMIT {$offset}, {$limit} ";
return $query; return $query;