I think you could simplify this a lot by using the pre_get_users
hook, instead of working directly with the complicated SQL within the pre_user_query
hook.
Try something like:
add_action( 'pre_get_users', function( \WP_User_Query $q )
{
$qv = &$q->query_vars;
if( isset( $qv['orderby'] ) && 'test_date' === $qv['orderby'] )
{
// Custom meta query
$qv['meta_query'] = [
'custom' => [
'key' => 'test_date',
'type' => 'DATETIME'
]
];
// Order by the 'custom' meta query
$qv['orderby'] = 'custom';
}
} );
where we’ve added the extra 'custom'
key in the meta query, so that we can order by it as well. This would assume all users to have the test_date
user meta key.
Hope you can adjust this accordingly.
Related Posts:
- Woocommerce Pre Orders view order link wrong [closed]
- Sort on meta value but include posts that don’t have one
- Using Orderby and meta_value_num to order numbers first then strings
- multiple orderby in pre_get_posts action
- get_the_terms in descending alphabetical order
- Ordering posts having multiple post-meta date fields
- next and previous post link ordered alphabetically
- Order posts by meta_value even if meta_key is not filled
- Order by first name
- Query between two meta values?
- How to alter query order direction using $query->set(‘order’, ‘ASC’); inside a pre_get_posts filter?
- the_tags() : display the tags by the order they are typed in in the backend, not alphabetically
- List taxonomy terms plus their latest post ordered by post date
- Sorting Attributes order when using get_the_terms
- How to order posts by meta value?
- How Do I Order Posts by Modified Date? [duplicate]
- Orderby Post Views
- How to order posts (woocommerce products) alphabetically but ignore certain words, eg ‘The’ and ‘An’
- How to order posts by alphabet with numbers at the end
- List of posts by day of the week
- WP User Query get all authors with last name starting with specific letter
- Best Plugin to Reorder Post Types
- Ordering system through WordPress
- Issue with multiple orderby values
- WP_Query ordered by meta_value_num doesn’t fetch posts without this meta field
- get_query_var returns wrong default value
- SQL: What is wrong with the following query (generated by WordPress WP_Query, ordering prices)
- Use pre_get_posts to sort meta_key by an array of values
- Show first the posts from 1 specific tag
- Order posts by price
- All, published and pending order
- Order by summing multiple values
- get_adjacent_post() returning weird results
- Get_Users Orderby Page
- Sort order of next/prev sibling page
- Order char by post title
- Why does pre_get_posts() return “date” as the orderby parameter for every sortable column?
- next_post_link and previous_post_link order not working properly
- query pages by a childs publish date
- List most recently added posts (with a twist)
- Check post_date in pre_get_posts
- Debugging wp_query orderby for taxonomy
- How can I prevent certain custom roles from seeing other custom roles on the user list page?
- pre_get_posts shows posts in random order sometimes
- Featured-Content/Featured Posts With Random Order
- Orderby in Query Posts affected by operating system?
- Ordering posts by title containing numbers
- last_name + first_name orderby with meta_query [solved]
- Stylesheet Enqueue Order and Best Practices
- wp_list_pages sort order by top nav menu order and child of
- How to display taxonomy terms the way they are hierarchical
- wp_list_categories() by alphabet? (ex. only category titles that start with “A”) [closed]
- WP_User_Query orderby meta_val_num
- How to add a date creation field when a custom taxonomy relationship is created?
- Randomly select one of last 3 posts
- Order by ASC, DESC, Title, Date from URL on custom page template post
- Offset posts with random order
- view subcategories order by id
- Sort query output on taxonomy term archive by post type using pre_get_posts
- ASC and DESC separately from orderby
- Order WP_Query by The Order of an Array in Tax_Query
- struggling to order by title
- Order by two meta values – one is a number and the other is text
- Last post sorting get_categories
- Compare time value in WP_User_Query for sending emails
- Sort by DESC works, ASC doesn’t?
- SQL to set Display Name to First Name + Last Name
- Shop sends Mail with order cancellation
- List of terms in alphabetical order under the respective initial letter and within columns
- update_post_meta saves data but does not show in Order backend
- sort title descending with title with number
- Changing cache update rate
- WP_User_Query Orderby Not Working
- how to complete order status automatically after paypal payment received
- Order management including recurring orders on woocommerce
- Orderby Title not working
- Sort loop by title doesn’t sort single digits like human would
- Woocommerce order_complete email dont send after changing order status via sql
- Sort posts by meta value with get method
- Why Am I getting an error when I add the number parameter to my WP_User_Query?
- Find by post_meta, then sort by post_meta, then sort by date (wp_posts)
- Custom post order when using OR relation
- Creating a post that contains a list of other posts like a table of contents
- Want to order by Meta Values (tweaking Post Order Widget)
- Changing posts order on a page
- How to order posts by the user_login that corresponds to a meta_value (which is a user id)?
- Pre_Get_Posts order DESC not registering
- orderby in WP_query doesn’t works
- Ordering Subcategories
- Sort users by custom user meta value
- How to order posts by parents?
- single post navigation order (NOT chronological)
- WP_Query with several meta_query-statements and order by meta_value
- Woocommerce: Grab total revenue of a product over all orders
- Problem with meta_value order after update
- Random order of posts on each request
- How can I make have_posts() return posts ordered DESC by id?
- Add post order like page order
- How to make WordPress orderby work with post_excerpt column?
- Orderby with menu_order and title