Last active
August 18, 2021 14:08
-
-
Save nayemDevs/f36117a8cd0ea6e0ed4eb1edcc6e2830 to your computer and use it in GitHub Desktop.
Random order on store-list page
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function dokan_store_listing_orderby_ramdom_filter( $seller_args ) { | |
$seller_args['orderby'] = 'dokan_store_rand'; | |
return $seller_args; | |
} | |
add_filter( 'dokan_seller_listing_args', 'dokan_store_listing_orderby_ramdom_filter', 99 ); | |
function dokan_store_listing_orderby_filter_query( $query ) { | |
if ( $query->query_vars['orderby'] === 'dokan_store_rand' ) { | |
$order_by = [ | |
'ID', | |
'user_login, ID', | |
'user_email', | |
'user_registered, ID', | |
'user_nicename, ID' | |
]; | |
if ( false === ( $selected_orderby = get_transient( 'dokan_store_listing_random_orderby' ) ) ) { | |
$selected_orderby = $order_by[ array_rand( $order_by, 1 ) ]; | |
set_transient( 'dokan_store_listing_random_orderby', $selected_orderby, MINUTE_IN_SECONDS * 10 ); | |
} | |
$query->query_orderby = "ORDER BY $selected_orderby"; | |
} | |
} | |
add_action( 'pre_user_query' , 'dokan_store_listing_orderby_filter_query' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I received a critical error when I added this to my child theme! What should I do?