-
Notifications
You must be signed in to change notification settings - Fork 36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[s2Member-List /] Search by Custom Profile Field #155
Comments
@raamdev If you feel like taking a look at this, please let me know. I'd love it if you could help me to make this possible. Here is the routine that we'll need to improve to get this working. What we need to do is find a way to get this method to search additional user fields; including custom field values stored by s2Member. I see this being quite challenging, but if you have some thoughts on the topic I'd love to hear them. See also: http://www.primothemes.com/forums/viewtopic.php?t=15658#p48878 |
Yes, I'll take a look at this and see if I can't get some workable code pushed up to a branch. By the looks of it, this will require running custom MySQL queries, is that correct? And if so, it probably makes sense to implement caching with WordPress transients to improve performance, right? Assigning this to myself. I'll plan to take a look at this later this week or early next week. |
Bump @jaswsinc (see questions above). |
Yes, that sounds like a plan to me. Honestly though, I have not thought this through completely. That is, I don't have anything specific in mind for this yet. Thus, whatever you can come up with that is a workable solution would be OK with me, since I don't really know what exactly needs to happen here just yet. If you start work on this I'd encourage you to just start pushing some ideas/concepts that you have in mind and maybe we can hash things out together. Whatever you think is fine :-) The outline you gave sounds like a great place to start. |
Another one requesting this: https://websharks.zendesk.com/agent/#/tickets/3896 |
I'll take a closer look at this today and see if I can come up with a workable solution. |
I'm not sure there's going to be any optimized way of handling this. If we assume that we'll need this to work on a site with thousands (perhaps tens of thousands) of users, then we're likely going to run into some serious performance concerns. The only way I can see this working is to run a routine prior to calling SELECT `user_id` as `ID` FROM `wp_usermeta` WHERE `meta_key` = 'wp_s2member_custom_fields' AND `meta_value` REGEXP '.*;s:[0-9]+:".*SEARCHTERM.*".*' That would give us all users who have an s2Member Custom Field with Then, with that list of User IDs, we could use the WP_User_Query // .....
if($args['number'] < 1) $args['number'] = 1; // Make sure this is always >= 1.
$args['offset'] = ($page - 1) * $args['number']; // Calculate dynamically.
// --------------------------------------------------------
/* Search database for any users with s2Member Custom Fields that contain SEARCHTERM in value */
global $wpdb;
$users = $wpdb->get_results ("SELECT `user_id` as `ID` FROM `" . $wpdb->usermeta . "` WHERE `meta_key` = '" . $wpdb->prefix . "s2member_custom_fields' AND `meta_value` REGEXP '.*;s:[0-9]+:\".*SEARCHTERM.*\".*'");
/* Build an array of all User IDs whose s2Member Custom Fields contain SEARCHTERM in their value */
$include_user_ids = array();
if (is_array ($users) && count ($users) > 0)
foreach ($users as $user)
$include_user_ids[] = $user->ID;
/* Add User IDs to a new include argument for WP_User_Query */
if(!empty($include_user_ids))
$args[] = array('include' => $include_user_ids);
// --------------------------------------------------------
$query = new WP_User_Query($args);
return array('query' => $query, 'pagination' => self::paginate($page, (integer)$query->get_total(), $args['number'])); This is untested code. I just wrote this off the top of my head as an idea for how this might work. Thoughts @jaswsinc? Do you want me to try implementing this, or are there bigger issues (perhaps related to performance) that you foresee? |
I agree. I think that's the best we can do. It sounds like a great plan to me! For the regex, you'll want something like this...
In PHP, that would be done like this... $search_col = 'country'; // Search for the `country` field.
$search_val = 'US'; // Search for this value in the `country` field.
$regex = '(^|;)s\:[0-9]+\:"'.preg_quote($search_col).'";s\:[0-9]+\:"'.preg_quote($search_val).'"';
$sql_regex = "REGEXP '".esc_sql($regex)."'";
// e.g. REGEXP '(^|;)s\\:[0-9]+\\:\"country\";s\\:[0-9]+\\:\"US\"' It might be possible to optimize this a little bit further by limiting the number of rows that need to be tested via regex, to those which match a simpler (slightly faster) LIKE match first.
|
If I read the code in Am I misunderstanding something about how the shortcode works? |
WordPress itself takes a By default, we search all columns. However, it's possible to provide a comma-delimited list of columns to search in the So for instance, something like this could search all fields, including s2 custom fields (all of them)...
Something like this would search only specific fields...
|
@jaswsinc
I'm looking over the code for this in I'm trying to figure out what the conditional would be to check if we're searching "all columns" (i.e., the Since we're setting default values for that argument, I can't just search if the Am I missing something here? |
If you're working from If you are working from If |
Here is the line where I fixed a bug related to default search columns. |
Yes, that's right. Those are all of the search-friendly columns supported by As you mentioned before, I think it's going to take additional/separate queries, since we can't rely upon |
Perfect! Thank you. |
Unfortunately, it appears that the I'm now looking into running two queries and merging them. This may actually require three queries (two searches merged and then one final query that we can paginate) because simply merging two queries would break pagination. |
@jaswsinc I've submitted a Pull Request for review (see wpsharks/s2member-pro#55). Here's how I got this working:
I've tested this and it appears to be working as expected, but I'd appreciate some help doing some additional testing. |
Oh, that sounds great! I'll review the PR shortly :-) Thanks!! |
Work from this issue went out with the release of s2Member v150102. |
Reopening this issue due to a bug that was brought to my attention by @brucewsinc. It looks like our work to add search fields added a bug by mistake. Pagination is no longer working as expected. We need to include the Referencing: https://github.com/websharks/s2member-pro/blob/000000-dev/s2member-pro/includes/classes/member-list.inc.php#L113 |
Got it. I'll work on this.
I'll also tackle that one. |
Improving `[s2Member-List /]` search functionality. Closes wpsharks/s2member#155, closes wpsharks/s2member#394
Next Release Changelog:
|
Copy. Thanks for taking over. :) I hadn't started--was caught up with... well, with everything else! |
This issue was resolved in the release of s2Member v150203. Please see: http://www.s2member.com/kb/v150203/ If there are any follow-ups needed, please open a new issue and we will investigate. Thanks! |
@clavaque writes...
No, only by columns in the
wp_users
table at this time, since that's what is supported byWP_User_Query
. However, we could alter this to improve things in a future release.The text was updated successfully, but these errors were encountered: