Thanks for the great question.Yea, you're running into a known
design flaw within s2Member. s2Member was designed to store all Custom Registration/Profile Fields configured with s2Member, into a serialized array in the
wp_usermeta table, under the meta_key
wp_s2member_custom_fields.
While this DOES have some nice advantages, it unfortunately has many disadvantages too. In your case, you're seeing that it's difficult to search your MySQL database for specific information in these fields, because they're all globbed together into one serialized array, stored with key
wp_s2member_custom_fields.
We are planning to address this issue in a future release of s2Member, in a way that preserves backward compatibility. Until then, if you need to search your MySQL database in this way, I suggest using the MySQL REGEXP operator, as seen below.
- Code: Select all
SELECT `user_id` as `ID` FROM `wp_usermeta` WHERE `meta_key` = 'wp_s2member_custom_fields' AND `meta_value` REGEXP '.*"my_unique_field_id";s:[0-9]+:"string value I am looking for".*'
So if I wanted all User IDs with Profile Field
country_code, with the value
US, I might do this.
- Code: Select all
SELECT `user_id` as `ID` FROM `wp_usermeta` WHERE `meta_key` = 'wp_s2member_custom_fields' AND `meta_value` REGEXP '.*"country_code";s:[0-9]+:"US".*'
Doing this "the WordPress way", it might look like this in your PHP code.
- Code: Select all
<?php
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 '.*\"country_code\";s:[0-9]+:\"US\".*'");
if (is_array ($users) && count ($users) > 0)
{
foreach ($users as $user)
{
$user = new WP_User ($user->ID);
print_r($user); }
}
?>
Reference articles ( might assist you with this ).
http://stackoverflow.com/questions/4116 ... ized-arrayhttp://dev.mysql.com/doc/refman/5.1/en/regexp.html