Skip to content

Instantly share code, notes, and snippets.

@nayemDevs
Last active August 18, 2021 14:08
Show Gist options
  • Save nayemDevs/f36117a8cd0ea6e0ed4eb1edcc6e2830 to your computer and use it in GitHub Desktop.
Save nayemDevs/f36117a8cd0ea6e0ed4eb1edcc6e2830 to your computer and use it in GitHub Desktop.
Random order on store-list page
<?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' );
@modabea
Copy link

modabea commented Aug 18, 2021

I received a critical error when I added this to my child theme! What should I do?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment