Title: SQL query: Get users by custom category value
Last modified: August 22, 2016

---

# SQL query: Get users by custom category value

 *  [Skygirl](https://wordpress.org/support/users/skygirl/)
 * (@skygirl)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/)
 * Hello,
 * I am trying to retrieve a list of members with a SQL query based on a particular
   custom category. Since the custom field values are stored as an array in wp_s2member_custom_fields,
   this proves to be challenging. How do I join this field (let’s call it corporate_category)
   to a typical sql query of users?
 * It’s easy enough to search for the value I seek anywhere within the custom_fields
   in the DB, but what I am running into is that that user’s value for the category
   may also exist in other custom category information, so I need a stricter search.
   For example,a query that pulls “Doctor” categorized users in this manner would
   also pull any user with, say, a custom description that said they carried scrubs
   for doctors.
 * Pardon me if this is a duplicate topic; I could not find anything exactly on 
   point. Thank you for your help!
 * [https://wordpress.org/plugins/s2member/](https://wordpress.org/plugins/s2member/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [krumch](https://wordpress.org/support/users/krumch/)
 * (@krumch)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399853)
 * Not really easy. You must use SQL commands to extract the value of ‘corporate_category’
   element in the wp_s2member_custom_fields column, which is saved as serialized
   array – a string, actually. So use the string functions of SQL to find ‘corporate_category’
   index in that string-array, and then to extract it’s value. Assign that value
   to a SQL variable, like
    `(some_complex_statement) as my_variable` Then you can
   use “my_variable” and compare/join it.
 *  Thread Starter [Skygirl](https://wordpress.org/support/users/skygirl/)
 * (@skygirl)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399925)
 * Thanks, krumch! The logic sounds great, but I’m new enough to SQL to not know
   how to find an index and its value in this array. Do I need to unserialize the
   array first? How does this fit into the original SQL query? Thank you for responding!
 *  [krumch](https://wordpress.org/support/users/krumch/)
 * (@krumch)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399936)
 * No, SQL have no command to unserialize an array, and no “array” value type there.
   You must handle it as a string, using string commands. There will be several 
   of them, included in each other. All this will be “some_complex_statement” I 
   show above. You should see how the array is saved in DB (my “[KC Tools](http://krumch.com/2012/05/10/kc-tools-wp/)”
   plugin is good for this) so you can find the string ‘”corporate_category”‘ inside,
   and then to find it’s value. Value is saved after (next to) the index, needs 
   more string commands to recognize and extract it. Yeah, not easy…
 *  Thread Starter [Skygirl](https://wordpress.org/support/users/skygirl/)
 * (@skygirl)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399937)
 * Thanks again! I have looked at the array in the DB, and to be honest the feature
   I’m trying to add isn’t worth the time this is taking. Getting users via WP queries/
   meta queries might do it if it were important enough to me. Necessity is the 
   mother of workarounds…
 * If anyone in S2Member development is reading this, here is a strong vote to store
   custom field values in their own columns in a future release. 🙂
 *  [KTS915](https://wordpress.org/support/users/kts915/)
 * (@kts915)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399938)
 * [@skygirl](https://wordpress.org/support/users/skygirl/), you can put your point
   directly to the s2Member developers [here](https://github.com/websharks/s2member/issues).
 *  Thread Starter [Skygirl](https://wordpress.org/support/users/skygirl/)
 * (@skygirl)
 * [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399940)
 * Will do, thank you!

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘SQL query: Get users by custom category value’ is closed to new replies.

 * ![](https://ps.w.org/s2member/assets/icon-256x256.png?rev=980067)
 * [s2Member – Excellent for All Kinds of Memberships, Content Restriction Paywalls & Member Access Subscriptions](https://wordpress.org/plugins/s2member/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/s2member/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/s2member/)
 * [Active Topics](https://wordpress.org/support/plugin/s2member/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/s2member/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/s2member/reviews/)

 * 6 replies
 * 3 participants
 * Last reply from: [Skygirl](https://wordpress.org/support/users/skygirl/)
 * Last activity: [11 years, 7 months ago](https://wordpress.org/support/topic/sql-query-get-users-by-custom-category-value/#post-5399940)
 * Status: not resolved