1

How do I change the last line to order by rand? Now it's sorting by date of my woocommerce products.

INNER JOIN $wpdb->postmeta AS mt1 ON (posts.ID = mt1.post_id)
    WHERE
        posts.post_status = 'publish'
        AND  (mt1.meta_key = '_sale_price_dates_to' AND mt1.meta_value >= ".time().")

        GROUP BY posts.ID
        **ORDER BY posts.post_date DESC";**
1
  • How are you making use of this SQL query? That's a very expensive query, but it can be done with WP_Query. Also note that ordering things randomly in an SQL query is hideously expensive. It has to create a brand new table in memory with the same rows but randomly ordered before it can even begin searching
    – Tom J Nowell
    Commented May 24, 2019 at 22:07

1 Answer 1

2

You can use ORDER BY rand(), but it's a very heavy query as a temporary table is created for it.

A WC_Product_Query or a WP_Query could be a good alternative to WPDB custom query. Both support 'orderby' => 'rand',

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.