Support

Account

Home Forums ACF PRO WP_User_Query search with meta_query for Post Object field in user meta data

Solving

WP_User_Query search with meta_query for Post Object field in user meta data

  • Hello – as my title states I am trying to make a WP_User_Query that uses the built in ‘search’ parameter of WP_User_Query to search for users on my site.

    It works as is, but I need to be able to add a meta_query that examines what the user has entered for a Post Object selection field on their profile.

    What I want is to be able to type the title of a post object in my search and have returned all users who have that post object set in their user meta post object ACF field.

    Does this make sense? Here is my query

    	$user_query = new WP_User_Query( array(
    		'fields'         => 'all',
    		'orderby'        => 'display_name',
    		'search'         => '*' . $term . '*',
    		'search_columns' => array( 'ID', 'user_login', 'user_email', 'user_nicename' ),
    		'meta_query' => array(
    		'relation' => 'OR',
    			array(
    				'key' => 'the_company', // name post object field
    				'value' => '"' . $term . '"',
    				'compare' => 'LIKE'
    			)
    		)
    	) ) );

    Any insight would be greatly appreciated.

  • What you are trying to accomplish is not directly possible. ACF only stores the post ID of the post object field. Using WP_User_Query there isn’t any way to look at the title of that post. For this to work that post title would need to be stored in the user meta table.

    The above is possible, you would need to create an acf/save_post filter. In this filter if a user is being saved then you would get the post object field and then get the title of the post and store it into another meta_key using update_post_meta(). Then you could use this new meta_key in your search.

    The main issue with doing this is that it would not be automatically updated if the post title was changed. This means that you’d need to create another filter that runs every time a post is saved. In this filter you’d need to do a query for all users that have selected the post and then update your new field to hold the correct title.

    Your other problem is that you cannot to an “OR” relationship between the standard WP search term and a meta query. It will only return results where the search term is in one of the standard search field AND it is in the meta value. This means the you need to also alter the WHERE clauses of the user query. I don’t have any information on doing this but it would be similar to searching a post where you want to do an OR with the post content or a meta field. I’ve done a quick search but I can’t find anything on how you’d go about doing this. This has come up before here, but I don’t think it was ever resolved https://support.advancedcustomfields.com/forums/topic/extend-backend-user-search-to-custom-user-meta/

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘WP_User_Query search with meta_query for Post Object field in user meta data’ is closed to new replies.