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
- Using a custom WP_Query with get_template_part loop
- How to make WP_Query ‘post__in’ accept an array?
- Is it possible to orderby multiple meta_keys when using meta_value_num?
- Is it possible to wrap Geo Location search around WP_Query?
- Merging multiple wp_query objects
- Query causing load because of SQL_CALC_FOUND_ROWS post counting?
- get data from wp-query, outside the loop & without url change
- Search another database with wp_query using new wpdb
- Querying by taxonomy vs Querying by Custom fields Speed Comparison
- Splitting the main query in multiple loops with query_posts and/or pre_get_posts?
- Order WP Query posts by custom order calculated from post meta values
- WP_Query mixe post and page attributes
- How do you Query posts with nothing in common?
- When to add_filter() to Custom Query
- if/else on custom query gives 200 OK when condition not met?
- Creating arguments from loop for WP_Query meta_query
- Can I set my WP_Query to be a Main Query?
- How to use external API content in place of WP posts using $wp_query?
- Make a page return false to is_single() and true for is_post_type_archive() before wp_enqueue_scripts
- wp_query args adding muitiple tax_querys
- WP_Query posts with comments only
- Default permalink structure causing Notice: Undefined property: WP_Query::$post
- How to check the array values, what WP_Query has brought to me?
- How to Order a list of taxonomies? orderby?
- Get posts that do not have the same tags as current
- queried_object using pre_get_posts gets notices and warning
- Expecting statement error from php loop using ACF plugin
- Get specific ACF key and value from all posts – no access to DB
- Query only displays one page_id
- How to exclude a category name from showing?
- I am officially missing something about transient posts
- Register Taxonomy – What is `query_var`?
- Get Post ID as a separate RSS feed item
- how to retrieve WP_Query without ordering by date [duplicate]
- Block internal search queries with pre_get_posts and regex rules
- Is it possible to give a classname to specific comments in the WordPress admin?
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- How to upload 3 attachments to current post?
- WP_Query for posts that have postmeta assigned to a taxonomy
- How to make a wordpress loop file that displays posts based on certain conditions
- Extend WP_User_Query or WP_User to return all custom fields?
- Custom loop – Isolating post meta output depending on current query taxonomy terms
- How do I display posts with specific value in a custom field into my loop?
- Orderby Meta Value and Query from Meta Query
- WP Query to order posts by multiple meta fields
- Order by multiple meta keys on wordpress
- Sort posts in dashboard using custom field; also include posts where field isn’t set
- taxonomy-{term}.php terms pagination returning 404 after a certain page
- Using WP Query, I want to include all posts in category 1 as long as they are not also in category 2
- Prevent URL Parameter Affecting other WP Query
- Sort by multiple columns using get_posts