Advertisement
fahimmurshed

Custom random products sorting filter for WooCommerce

Jun 10th, 2023
694
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.42 KB | None | 0 0
  1. /**
  2.  * Custom random products sorting filter for WooCommerce
  3.  */
  4.  
  5. /**
  6.  * Modify the product query to sort by random order
  7.  *
  8.  * @param WP_Query $q The WP_Query instance.
  9.  */
  10. function custom_random_products_sorting( $q ) {
  11.     if ( ! is_admin() && $q->is_main_query() && is_woocommerce() && is_shop() ) {
  12.         $q->set( 'orderby', 'rand' );
  13.     }
  14. }
  15. add_action( 'pre_get_posts', 'custom_random_products_sorting' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement