The pre_user_query action hook in WordPress will allow you alter the SQL statements in the WP_User_Query object before they are run against the database. Note, this is an action, not a filter, so there’s no need to return the $user_query that gets passed in.
add_action( 'pre_user_query', 'add_my_custom_queries' );
function add_my_custom_queries( $user_query ) {
$user_query->query_fields .= ', my_custom_field '; // additional fields
$user_query->query_from .= ' INNER JOIN my_table '; // additional joins here
$user_query->query_where .= ' AND field='value' '; // additional where clauses
$user_query->query_orderby .= ' ORDER BY my_custom_field '; // additional sorting
$user_query->query_limit .= ''; // if you need to adjust paging
}
Related Posts:
- Getting all user metadata from user
- WP User Query get all authors with last name starting with specific letter
- Reduce number of SQL queries inside WP_Query loop to fetch author data
- Random users always showing same 8 users
- How to query 5 users in random who have published more than 10 posts
- Query Users by post count, last 30 days and display each users post count according to post type
- Searching user meta using WP_User_Query
- Filter user list that meta_value is empty
- Query wp_usermeta alongside wp_users
- SQL User Query by Multiple Roles using PHP
- WP_User_Query unable to retreive all user at once
- Sort users by “birthday” using WP_User_Query
- Hide Administrators From User List except current user (administrator)
- WordPress search posts by author name with autocomplete
- pagination with numbers for WP_User_Query
- How to display user order by role
- Import wp users via one click demo option
- Dynamic User Id
- Get users that likes the post
- Prevent author bio page from showing in search results
- Get multiple users with meta value in one query and populate WP_User class
- Display if author page is author page of current user
- array of meta values using WP_User_Query
- Get user first name from custom endpoint
- How to properly use AND / OR in custom search
- WP_User_Query and user posts
- construct complex queries with WP User Query
- Storing an array of objects related to each user
- Create one-use post dynamically, add to main query, do not insert post (user profile view)
- WP_User_Query orderby meta_val_num
- Query to get last posts from authors of blog
- Replace wp_query with wp_user_query
- Query pages created by deleted users
- Query posts by post type, author and post meta
- SQL to join u3g_users & u3g_meta_value with repeating data
- How to do WP_User_Query for Nested Array value Inside Nested Array field
- WP_User_Query Orderby Not Working
- Set “editor” role to existing user
- How to query users by post count no less than 10
- Merge wp-query and get_users
- Get all wp_users sort by metakey
- Logging search queries for logged-in users
- WP_User_Query not searching numbers?
- List users by Year of Birth using a foreach loop to dynamically populate years and data
- Show subscriber id in loop
- Search Functionality broken by the wp 4.2 update
- Get users query makes the site loading time too big?
- Get all user with both meta_value
- WordPress WP_User_Query ignores the `meta_query` when called from an Ajax function
- Orderby meta _value that is saved as an array
- Group users by meta field, with name of meta field as group title
- Complicated `orderby` based on text in custom field
- Using a Query Loop Block with Variables (Custom Meta)
- When to use WP_query(), query_posts() and pre_get_posts
- WP query taxonomy input differs to output?
- WP_Query min and max values
- querying with custom meta field with meta_query
- Order by two meta keys
- multiple meta key but get server load is very high
- How to bring specific post to front of wordpress loop?
- How can i get the last post from wp multisite?
- Use meta_query to display events by date in custom field
- tax_query OR with empty result returns all posts
- WP_Query Order by Specific Post ID First
- Custom Query – show first post twice?
- Using one WP_Query object within the loop of another WP_Query object
- Order by slug in get_terms with multiple taxonomies
- Is there any advantage of using default WordPress Search instead of creating a custom one?
- is_tax() function not working as expected
- posts_per_page not working for first page of pagination
- Add class to first post in custom loop using post_class filter
- echo a specific meta_key queried through a custom post
- WP_Query with meta_query dosen’t return results
- meta_compare not comparing whole integer
- WP_Query with rewind_posts creates duplicate titles
- Transients with dynamic WP_Query
- ajax wp_query conditional tags not working
- Comparing 2 Decimal Numbers from custom fields and displaying posts
- max_num_pages says 4, yet I am only able to navigate to the third page
- paginate_links appearing on page but it doesn’t actually paginate – pagination on a static page with a dynamic `post_type` argument on a static page
- Form checkbox value going to dynamic URL
- show most viewed post
- WordPress Carousel with recive posts (Bootstrap)
- Pre_get_posts only show posts by administrator roles
- WP Query – Search in title or author_name
- WP Query for Multiple Post Types
- URL to Post Archive of Custom Query
- Query by Category and Custom Field – Shortcode
- Pagination returns 404 after page 20
- WP_Query a custom field value bringing back 0 results
- Year based WP Query
- Creating array to compare custom field values
- WordPress Query Default Order if Meta Values Same?
- Comments orderby comment date not working
- Wp query by 2 meta field – check if they exists / have value
- Show linked products using wp query and woocommerce
- Standard Loop – wp_query
- WP Query – if there are no posts with certain tag query other ones
- How does ‘category__not_in’ differ to cat => ‘-1’?
- Custom Wp_query loop takes very long