Support » Plugin: Simple User Listing » Query multiple meta keys & custom fields

  • hmnstd

    (@hmnstd)


    I need to be able to search the entire usermeta table and have followed your advice under https://wordpress.org/plugins/simple-user-listing/#how%20to%20create%20very%20complex%20user%20queries%20%7C%20how%20to%20query%20multiple%20meta%20keys

    Here is my code, I have some custom fields in the usermeta table but the plugin doesn’t seem to be searching on any of them:

    add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );
    
    function sul_custom_meta_query( $args, $query_id ){
        // checking the query ID allows us to only target a specific shortcode
        if( $query_id == 'my_custom_meta_query' ){ 
                $args['meta_query'] = array(
                    'relation' => 'OR',
    	                array(
    			            'key' => 'first_name',
    			            'value' => $search,
    			            'compare' => '=',
    			        ), 
    			        array(
    			            'key' => 'last_name',
    			            'value' => $search,
    			            'compare' => '=',
    			        ), 
    			        array(
    			            'key' => 'institution',
    			            'value' => $search,
    			            'compare' => '=',
    			        ), 
    			        array(
    			            'key' => 'interests',
    			            'value' => $search,
    			            'compare' => '=',
    			        ),  
    			        array(
    			            'key' => 'requirements',
    			            'value' => $search,
    			            'compare' => '=',
    			        ),  
    			    
                );
        	}
        return $args;
    }

    I’m using the shortcode [userlist query_id=”my_custom_meta_query”]

    • This topic was modified 2 years ago by hmnstd.
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Author helgatheviking

    (@helgatheviking)

    I’m away from my keyboard, but where are you defining $search?

    hmnstd

    (@hmnstd)

    My mistake. $search was taken from the original plugin at https://www.smashingmagazine.com/2012/06/front-end-author-listing-user-search-wordpress/

    Is there a way I can have search terms in the function?

    Plugin Author helgatheviking

    (@helgatheviking)

    Same as in the tutorial and in the plugin, you need to define the variable from the $_GET or $_POST.

    // Get the Search Term
    $search = ( isset($_GET["as"]) ) ? sanitize_text_field($_GET["as"]) : false ;

    Then if you have a $search you can add the meta query conditionally.

    hmnstd

    (@hmnstd)

    So like this? If so, it won’t search on my custom field, or a combination of first and last name.

    add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );
    
    function sul_custom_meta_query( $args, $query_id ){
        
        if( $query_id == 'my_custom_meta_query' ){ 
    	    
    	    	$search = ( isset($_GET["as"]) ) ? sanitize_text_field($_GET["as"]) : false ;
    	    	
                $args['meta_query'] = array(
                    'relation' => 'OR',
    	                array(
    			            'key' => 'first_name',
    			            'compare' => 'LIKE',
    			            'value' => $search                                      
    			        ),          
    			        array(
    			            'key' => 'last_name',
    			            'compare' => 'LIKE',
    			            'value' => $search                                      
    			        ),    
    			        array(
    			            'key' => 'institution',
    			            'compare' => 'LIKE',
    			            'value' => $search                                       
    			        ) 
    			);
        	}
        	
        return $args;
    }
    Plugin Author helgatheviking

    (@helgatheviking)

    Maybe try this?

    
    add_filter( 'sul_user_query_args', 'sul_custom_meta_query', 10, 2 );
    
    function sul_custom_meta_query( $args, $query_id ){
    
    	if( $query_id == 'my_custom_meta_query' ){ 
    	
    		$search = ( isset($_GET["as"]) ) ? sanitize_text_field($_GET["as"] ) : false ;
    			
    		if( $search ) {
    			$args['meta_query'] = array(
    				'relation' => 'OR',
    				array(
    					'key' => 'first_name',
    					'compare' => 'LIKE',
    					'value' => $search
    				),
    				array(
    					'key' => 'last_name',
    					'compare' => 'LIKE',
    					'value' => $search
    				),
    				array(
    					'key' => 'institution',
    					'compare' => 'LIKE',
    					'value' => $search 
    				) 
    			);
    
    			unset( $args['search'] );
    
    		}
    	}
    	
    	return $args;
    }
    

    I think the thing I was forgetting was that if you want to run the meta query off of a search, then you need to remove the plugin’s default search parameter (the unset( $args['search' ] ) bit). Otherwise, I think you’ll be in a search plus meta query and you’ll never get a result. Keep in mind, that based on my testing if you enter a person’s full name, ex: Diana Prince you may not get a result even if the first_name is Diana and the last_name is Prince. I don’t fully understand why that is or how to fix it via meta queries.

    As an alternative you could force the users to have their first and last names set as their “user_nicename” and then you should be able to search by that without meta. See WP_User_Query search columns: https://codex.wordpress.org/Class_Reference/WP_User_Query#Search_Parameters

    hmnstd

    (@hmnstd)

    Thanks. I have a lot of fields on user profiles and need to be able to search on them all. This seems to be a reoccuring problem with user meta queries it seems. I have tested the plugin Relevanssi Premium and it does the trick. Thanks again.

    Plugin Author helgatheviking

    (@helgatheviking)

    Searching by a lot of meta fields is probably very inefficient and you’re better off using a special plugin. I’m glad Relevanssi is doing the trick.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Query multiple meta keys & custom fields’ is closed to new replies.