-
Notifications
You must be signed in to change notification settings - Fork 0
/
queries-patterns.php
70 lines (60 loc) · 2 KB
/
queries-patterns.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Multi-Sorting of Users:
* 1 - the first sorting is by related custom post-type title, its ID saved as User meta-field;
* 2 - the second sorting is by login
*/
add_action('pre_get_users', 'get_custom_sorted_users');
/**
* Set query parameters according to needed fields
*
* @param $query WP_User_Query
* @return WP_User_Query
*/
function get_custom_sorted_users( $query ) : WP_User_Query
{
// Allow only for wp-admin
if( !is_admin() ) return $query;
// Allow only for Users List page
$screen = get_current_screen();
if ( isset( $screen->id ) && $screen->id !== 'users' ) return $query;
// Ordering by the title of related_post_type
if ( $query->get( 'orderby' ) == 'related_post_type' ) {
// Create double order
$query->set( 'order', $_GET[ 'order' ] . ' asc' );
// Set order by include and login
$query->set( 'orderby', 'include login' );
// Retrieve included IDs
$query->set( 'include', get_sorted_users( $query, 'related_post_type', $_GET[ 'order' ] ) );
}
}
/**
* Returns sorted array with Users IDs according to the Query
*
* @param $query WP_User_Query
* @param $field string
* @param $order string
*
* @return array
*/
function get_sorted_users( $query, $field, $order ) : array
{
// Get Users with needed role
$all_users = get_users([ 'role' => $query->query_vars['role'] ]);
// Create $sorted array and fill-up it with pairs user_id->post_title of custom_post_type
$sorted = [];
if ( is_array($all_users) ) {
foreach ( $all_users as $user ) {
$related_post_title = get_the_title( intval( get_user_meta($user->ID, $field, true) ) );
if ( empty($related_post_title) ) $related_post_title = 'zzz'; //last in sorting
$sorted[$user->ID] = $related_post_title;
}
}
if ( $order == 'asc' ) {
asort($sorted, SORT_STRING);
}
if ( $order == 'desc' ) {
arsort($sorted, SORT_STRING);
}
return array_keys( $sorted );
}