• Resolved heatstroke

    (@heatstroke)


    Hi Helga,

    sorry to be the noisy one these days πŸ™‚

    Is it possible to make this plugin search by multiple fields at the same time?

    I have a lot of users and I want it to search at least by first names and last names. This way, if I search for James, it will filter by first name. If a search for Bond… it will retrieve a list of users with last name Bond.

    Thank you a lot πŸ˜‰

    http://wordpress.org/plugins/simple-user-listing/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author HelgaTheViking

    (@helgatheviking)

    Do you mean multiple search inputs? or 1 input that searches 2 meta fields? Either way, the answer is probably “yes, it’s possible.” But I don’t really have the answer, nor do I provide support on how to implement heavy customization of SUL.

    This gist might help: https://gist.github.com/helgatheviking/5943857

    As it sets up a search by a meta field… which you could use to add a second input. And the “mutiple custom user fields” section of the codex might help if you wish to query by two meta fields:
    http://codex.wordpress.org/Class_Reference/WP_User_Query#Custom_Field_Parameters

    Best of luck.

    Thread Starter heatstroke

    (@heatstroke)

    Ok,

    that would be one of the best features of the plugin, as nobody searches for usernames. People search for real names and surnames, no nicks.

    I tried the code you linked. It is the same es the one you posted in the FAQ section. It works when searching by one meta_key. The main problem for me is to use two meta_keys instead of one and make the search from one textfield.

    Anyway, I will keep trying it.

    Thank you πŸ˜‰

    Thread Starter heatstroke

    (@heatstroke)

    Hi people… 15 minutos later I did it. If someone needs the same, take note of this code. Now you can search by first name or last name in the same textfield.

    This is how I did it:

    search-author.php

    <?php
    /**
     * The Template for displaying Author Search
     *
     * Override this template by copying it to yourtheme/simple-user-listing/search-author.php
     *
     */
    
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
    
    $search = ( get_query_var( 'first_name' ) ) ? get_query_var( 'first_name' )  : '';
    
    ?>
    
    <div class="author-search">
    	<h2><?php _e('Search authors by name' ,'simple-user-listing');?></h2>
    		<form method="get" id="sul-searchform" action="<?php the_permalink() ?>">
    			<label for="as" class="assistive-text"><?php _e('Search' ,'simple-user-listing');?></label>
    			<input type="text" class="field" name="first_name" id="sul-s" placeholder="<?php _e('Search Authors' ,'simple-user-listing');?>" value="<?php echo $search; ?>"/>
    			<input type="submit" class="submit" id="sul-searchsubmit" value="<?php _e('Search Authors' ,'simple-user-listing');?>" />
    		</form>
    	<?php
    	if( $search ){ ?>
    		<h2 ><?php printf( __('Search Results for: %s' ,'simple-user-listing'), '<em>' . $search .'</em>' );?></h2>
    		<a href="<?php the_permalink(); ?>"><?php _e('Back To Author Listing' ,'simple-user-listing');?></a>
    	<?php } ?>
    </div><!-- .author-search -->

    functions.php

    // Switch the WP_User_Query args to a meta search
    function kia_meta_search( $args ){
      // this $_GET is the name field of the custom input in search-author.php
        $search = ( isset($_GET['first_name'])) ? sanitize_text_field($_GET['first_name']) : false ;
        if ( $search ){
            // if your shortcode has a 'role' parameter defined it will be maintained
            // unless you choose to unset the role parameter by uncommenting the following
            //  unset( $args['role'] );
    		$args = array(
    	'meta_query' => array(
    		'relation' => 'OR',
    		array(
    			'key'     => 'last_name',
    			'value'   => $search,
    			'compare' => 'LIKE'
    		),
    		array(
    			'key'     => 'first_name',
    			'value'   => $search,
    			'compare' => 'LIKE'
    		)
    	)
     );
        }
        return $args;
    }
    add_filter('sul_user_query_args', 'kia_meta_search');
    
    // Register query var and whitelist with Simple User Listing
    function kia_search_vars( $vars ){
        $vars[] = 'first_name';
        return $vars;
    }
    add_filter('sul_user_allowed_search_vars', 'kia_search_vars');

    Hope it helps πŸ˜‰

    Plugin Author HelgaTheViking

    (@helgatheviking)

    Thanks for sharing your results. FWIW, if you aren’t adding another search input, then you don’t really need to override search-author.php, just adjust the $_GET accordingly.

    I’ve heard that this would be slow for lots and lots of users, but it does probably make more sense than search by user login. That’s more of a limitation of WP and WP_User_Query than anything else though.

    In the future, I might add the ability to at least change the “search by” from the shortcode ( at least to the different types that are currently supported by WP_User_Query ). However, as with all my plugins at the moment, they aren’t really in “active” development, so I couldn’t predict when that would be.

    Thread Starter heatstroke

    (@heatstroke)

    True, no need to change the search-author.php file, just realised it.

    This may be slower, but it is very important to me. I tried to add one more meta_key, like description, but then it became much slower, so I left only the two, first_name and last_name.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search by multiple fields’ is closed to new replies.