Created
September 19, 2020 08:48
-
-
Save toptools/f1cce15d903ffdf106f5e02e23167f2f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
add_action( 'restrict_manage_posts', 'shop_order_user_role_filter' ); | |
function shop_order_user_role_filter() { | |
global $typenow, $wp_query; | |
if ( in_array( $typenow, wc_get_order_types( 'order-meta-boxes' ) ) ) : | |
$user_role = ''; | |
// Get all user roles | |
$user_roles = array(); | |
foreach ( get_editable_roles() as $key => $values ) : | |
$user_roles[ $key ] = $values['name']; | |
endforeach; | |
// Set a selected user role | |
if ( ! empty( $_GET['_user_role'] ) ) { | |
$user_role = sanitize_text_field( $_GET['_user_role'] ); | |
} | |
// Display drop down | |
?><select name='_user_role'> | |
<option value=''><?php _e( 'Select a user role', 'woocommerce' ); ?></option><?php | |
foreach ( $user_roles as $key => $value ) : | |
?><option <?php selected( $user_role, $key ); ?> value='<?php echo $key; ?>'><?php echo $value; ?></option><?php | |
endforeach; | |
?></select><?php | |
endif; | |
} | |
add_filter( 'pre_get_posts', 'shop_order_user_role_posts_where' ); | |
function shop_order_user_role_posts_where( $query ) { | |
if ( ! $query->is_main_query() || ! isset( $_GET['_user_role'] ) ) { | |
return; | |
} | |
$ids = get_users( array( 'role' => sanitize_text_field( $_GET['_user_role'] ), 'fields' => 'ID' ) ); | |
$ids = array_map( 'absint', $ids ); | |
$query->set( 'meta_query', array( | |
array( | |
'key' => '_customer_user', | |
'compare' => 'IN', | |
'value' => $ids, | |
) | |
) ); | |
if ( empty( $ids ) ) { | |
$query->set( 'posts_per_page', 0 ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment