0

I have a user base of hundreds of users, and trying to create a custom API endpoint to get users in a specific membership group.

The database structure is as follows (using Ultimate Member plugin):

  • User is stored in wpma_users table
  • User metadata in table wpma_usermeta contains user meta named mygroups, containing a value a:1:{i:0;s:3:"155";}
  • The 155 value is the ID of a term

I already tried this, with $groupid=155:

$args = array(
            'fields'    => 'all_with_meta',
            'tax_query' => array(
                array(
                  'taxonomy' => 'mygroups',
                  'field' => 'term_id',
                  'terms' => $groupid,
                ),
            ),
);
$user_query = new WP_User_Query($args);

When I var_dump get_terms(array('include' => $groupid)), an actual term is printed:

array(1){
    [
        0
    ]=>object(WP_Term)#27633(10){
        [
            "term_id"
        ]=>int(155)[
            "name"
        ]=>string(8)"Example Group"[
            "slug"
        ]=>string(8)"example_group"[
            "term_group"
        ]=>int(0)[
            "term_taxonomy_id"
        ]=>int(155)[
            "taxonomy"
        ]=>string(11)"um_user_tag"[
            "description"
        ]=>string(0)""[
            "parent"
        ]=>int(149)[
            "count"
        ]=>int(1)[
            "filter"
        ]=>string(3)"raw"
    }
}

Another way I tried, is using the meta_query instead of the tax_query:

'meta_query' => array(
            array(
                'key' => 'mygroups',
                'value' => $groupid,
                'compare' => 'LIKE',
            )
),

This actually filters the query, but when I use $groupid=1, it filters where groupid contains 1, so also 10, 11, 100 etc.

Who can point me in the right direction to filter the user on mygroups the right way?

1
  • 2
    You need to ask this in an Ultimate Member plugin community, this isn't possible using generic WordPress
    – Tom J Nowell
    Commented Sep 23, 2022 at 12:24

2 Answers 2

3

This isn't possible, you can't safely/reliably query the internals of meta value strings that way, and you can't use it as a form of indirection:

  • You can query for substrings or for whole values, but you can't query the internals of serialised PHP values without major performance reliability and consistency issues
  • but if you could, or if it was just the plain number, there's no way to then take that and plug it into a term query
  • You can only use tax_query to find users in a given taxonomy, but your users don't have terms, and they aren't in a taxonomy.

User taxonomies are a thing, so be careful about what you're asking. Instead, you need to rephrase your question like this:

How do I find users with a specific value that's listed in the mygroups user meta field using WP_User_Query?

And the answer is, you can't. This is because mygroups is stored as a serialised PHP array, and you can't query sub-values of serialised values. E.g. you could use a LIKE style parameter to search for group 1 but it will also give you 10, 100, 41, etc It may even give you users who have a group number that is 1 digit long, and users that have 1 group even if that groups number is 9999.

It may be that there is a solution that uses features of the Ultimate Member plugin, but questions about that plugin and other 3rd party plugins are off-topic here and not in this stacks scope

1
  • Thanks for your extensive answer! Also, thanks for your clarification about taxonomies and terms. Since I could get specific terms with the Wordpress get_terms($id) method, I thought it would maybe be possible with native Wordpress filtering. I'll try the Ultimate Member form.
    – harmjanr
    Commented Sep 24, 2022 at 16:52
0

I found a solution, since the serialized array is always structured like this: ':"%s";', it is possible to write a meta_query like:

'meta_query' => array(
    array(
        'key' => 'mygroups',
        'value' => sprintf(':"%s";', $groupid),
        'compare' => 'LIKE'
    )
)

Works like a charm!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.