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)
- How to remove “MY WORDPRESS”” Label
- Search filter between promo and exact price
- query hook parse_tax_query function takes no effect
- Order post by modified date
- meta_query with meta values as serialize arrays
- Is it possible to give a classname to specific comments in the WordPress admin?
- Populate select list with meta values from all posts of a Custom Post Type
- pre_get_posts pagination not working
- Prevent Duplicate Post Counted by Query
- How can I have sticky posts while ALSO showing posts from a specific category using one WP_Query?
- tax_query not working on custom post type
- Filter for each loop when WP_Query has no posts to show
- Save queried result into database
- Show child by slug, while knowing parent ID
- sanitize_post() is not sanitizing Post Object
- Form checkbox value going to dynamic URL
- get_post_format is not returning standard
- Duplice post with standard WP loop – fixed by using query_posts() instead
- How to use the Term Object from a custom select field in a query
- ElasticPress is (aparently) messing with my search filters
- get_posts function not returning any results
- how to include orderby value that is empty?
- Why last row deleted when refresh page
- meta_query – check for multiple meta values in key which holds an array of values
- SQL to set Display Name to First Name + Last Name
- Custom Taxonomy – Modify Function to Get Child Category
- How to sort wordpress posts already selected by WP_QUERY
- WP Query – grouping posts by same meta key, adding together values from another key
- Pagination repeating posts on search results
- I have problems with the search query using multiple post types
- setting offset to category number in archive page
- Woocommerce Get Orders By Meta Value
- Retrieve all custom field values of a specific custom field metakey as an array, inside WP_Query
- Cannot update newly added User field using wp_update_user
- Pagination not working on archive.php template?
- WordPress Sub Category Archive, Show Extra Empty Page Number in Pagination
- Save large WP_Query to transient === false
- How to grab data after wp user search is complete
- WP_Query breaking the loop in a nested loop
- ACF: How can I publish values of ACF fields in a loop while using wp_query?
- 3 posts from each existing category on one page
- WordPress Query for CPT that only shows posts within radius of current user’s geolocation
- Sort by price wpdb
- WP_Query for a taxonomy value OR a custom post type meta field
- How to make WPQuery custom post type work in Twig / Timber
- Meta query ignores multiple values of the key
- Posts per Page not working