Related posts - replace ORDER BY RAND() with random offset.

Closes #7436
This commit is contained in:
Mike Jolley 2015-02-16 13:16:18 +00:00
parent c6c7952520
commit 5c1a0d92e8
2 changed files with 16 additions and 2 deletions

View File

@ -1237,6 +1237,8 @@ class WC_Product {
public function get_related( $limit = 5 ) {
global $wpdb;
$limit = absint( $limit );
// Related products are found from category and tag
$tags_array = array(0);
$cats_array = array(0);
@ -1298,8 +1300,19 @@ class WC_Product {
$query['where'] .= " AND p.ID NOT IN ( " . implode( ',', $exclude_ids ) . " ) )";
}
$query['orderby'] = " ORDER BY RAND()";
$query['limits'] = " LIMIT " . absint( $limit ) . " ";
// 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, YEAR_IN_SECONDS );
}
// Generate limit
$offset = absint( rand( 0, $max_related_posts - $limit ) );
$query['limits'] = " LIMIT {$offset}, {$limit} ";
// Get the posts
$related_posts = $wpdb->get_col( implode( ' ', apply_filters( 'woocommerce_product_related_posts_query', $query, $this->id ) ) );

View File

@ -155,6 +155,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc
* Tweak - Register shop_order post statuses earlier to ensure statuses are registered for cron.
* Tweak - Improvements to refund handling in Taxes by code/date, and sales by date reports. Gross/net excludes refunds.
* Tweak - Share data between Sales by Date report and API.
* Tweak - Related posts - replace ORDER BY RAND() with random offset.
= 2.3.3 - 12/02/2015 =
* Fix - Potential notice with preg_match wildcard search, if used incorrectly.