WordPress.org

Ready to get started?Download WordPress

Forums

Simple User Listing
Search by last name (7 posts)

  1. acrane
    Member
    Posted 9 months ago #

    I must be missing something very simple. I've tried both this thread and your faq's, both different approaches to search by last name with no luck.

    echo do_shortcode('[userlist role="churchmember" meta_key="last_name"]');

    I've also tried:
    meta_key="user_lastname" from the get_userdata()

    Nothing.

    I ultimately want to search by my custom user fields of , first name, last name, birthdate, and a custom taxonomy of family applied to the users, but I can't seem to get passed searching for last name. :(

    I'm playing around here.

    Thanks if you have any suggestions.

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

  2. helgatheviking
    Member
    Plugin Author

    Posted 9 months ago #

    The shortcode meta_key parameter doesn't change the search function. As is shown in the FAQ you must filter the query args.

    I've also written an walk-through of a very custom search in this tutorial: http://www.kathyisawesome.com/489/simple-user-listing/

    Though I think the FAQ covers what you are doing. Simply swapping the default 'as' input for the custom 'billing_city' example (meaning: don't change the search form) and swapping 'last_name' for the 'billing_city' meta_key:

    /**
     * Place this in your theme's functions.php file
     * Or a site-specific plugin
     *
     */
    // 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['as']) ) ? sanitize_text_field($_GET['as']) : 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['meta_key'] = 'last_name';
            $args['meta_value'] = $search;
            $args['meta_compare'] = '=';
        }
    
        return $args;
    }
    add_filter('sul_user_query_args', 'kia_meta_search');

    Totally untested, but should be on the right path.

  3. adamrafferty
    Member
    Posted 7 months ago #

    HI Adam here.

    Can I do a search for first name, or last name, or display name - for example?

    Thanks

  4. helgatheviking
    Member
    Plugin Author

    Posted 7 months ago #

    Take a look at the FAQ... particularly the "How can I setup custom search? (search by a meta field)" section (though you will have to modify the meta field as my example is "billing_city").

  5. helgatheviking
    Member
    Plugin Author

    Posted 7 months ago #

    Re-reading your comment, for some reason I didn't realize it was a follow-up to an existing thread.

    Anyway, you want to search by 3 meta fields. I presume this is possible, but I don't know how.... probably in setting up an advanced meta_query attribute similar to how it is done for posts. If you figure it out do share your solution. I will probably add it to the FAQ.

  6. helgatheviking
    Member
    Plugin Author

    Posted 7 months ago #

    @acrane I have since tested the above code and it is missing a line. The original search args must be removed or you will return an empty set of users.

    /**
     * Place this in your theme's functions.php file
     * Or a site-specific plugin
     *
     */
    // 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['as']) ) ? sanitize_text_field($_GET['as']) : 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['meta_key'] = 'last_name';
                    $args['meta_value'] = $search;
                    $args['meta_compare'] = '=';
    
    		// need to unset the original search args
    		if( isset( $args['search'] ) ) unset($args['search']);
    	}
    
    	return $args;
    }
    add_filter('sul_user_query_args', 'kia_meta_search');
  7. helgatheviking
    Member
    Plugin Author

    Posted 7 months ago #

    @adam

    In theory, http://codex.wordpress.org/Class_Reference/WP_User_Query shows how to list users by multiple custom fields ( see the section: "Multiple custom user fields handling" ). However, I can't get it to work and it always returns all the users.

    Maybe you will have better luck.

Reply

You must log in to post.

About this Plugin

About this Topic

Tags

No tags yet.