• Resolved candell

    (@candell)


    Hi

    I am displaying a list of users that have a value for a meta field ‘offer’;

    $usersWithOffer = get_users(array(
                    'meta_key' => 'offer',
                    'meta_compare'  =>  '!==',
                    'meta_value' => ''
                ));

    I also need to order this list by another meta field ‘business’.

    I added the other key and tried to order by it but it isn;t working

    $usersWithOffer = get_users(array(
                    'meta_key' => 'offer',
                    'meta_compare'  =>  '!==',
                    'meta_value' => '',
                    'meta_key' => 'business',
                'orderby' => 'business',
                'order' => 'ASC'
                ));

    Can anyone let me know where I have gone wrong?

    Thanks

Viewing 6 replies - 1 through 6 (of 6 total)
  • Please try the following code:

    $args = array(
        'meta_query' => array(
            array(
                'key' => 'offer',
                'value' => '',
                'compare' => '!=='
            ),
            array(
                'key' => 'business',
                'value' => '',
                'compare' => '!=='
            )
        )
    );
    $usersWithOffer = get_users($args);

    I hope it will solve your problem.

    Thread Starter candell

    (@candell)

    Hi

    Thanks for the help, unfortunately it isn’t ordering by business still.

    Moderator bcworkz

    (@bcworkz)

    According to docs, '!==' is not a valid compare value, only '!='. I’ve not investigated further. In any case, you can use 'NOT EXISTS' instead. Then AFAIK no value argument is needed. If you really need to check type not match with !==, use the ‘type’ argument to cast the value to whatever it’s supposed to be.

    To order by a meta value, the main args array needs a “meta_key” arg outside of the “meta_query” arg arrays. Don’t provide an accompanying “meta_value” arg or it’ll conflict with the “meta_query”. I think you only want ordering by business, not to qualify the results by it. The only qualification would be for “offer” not existing, correct?

    If so I think this is what you need:

    $args = array(
        'meta_key' => 'business',
        'meta_query' => array(
            array(
                'key' => 'offer',
                'compare' => 'NOT EXISTS',
            ),
        ),
        'order_by' => 'meta_value',
    );
    $usersWithOffer = get_users($args);

    If you still have trouble, we should examine the resulting SQL to determine the problem. We may need to filter the actual SQL to correct any portion that is not as desired.

    Thread Starter candell

    (@candell)

    Thank you for the reply, I want to show users who do have a value set under offer.

    I changed your ‘compare’ => ‘NOT EXISTS’, to ‘compare’ => ‘EXISTS’,

    Here’s the generated query, it seems to be ignoring order by

    SELECT wpsc_users.*
    FROM wpsc_users
    INNER JOIN wpsc_usermeta
    ON ( wpsc_users.ID = wpsc_usermeta.user_id )
    INNER JOIN wpsc_usermeta AS mt1
    ON ( wpsc_users.ID = mt1.user_id )
    WHERE 1=1
    AND ( wpsc_usermeta.meta_key = 'business'
    AND ( mt1.meta_key = 'offer' ) )
    ORDER BY user_login ASC

    whereas it should be ordering by ‘business’ which is a meta field.

    Moderator bcworkz

    (@bcworkz)

    Argh, my bad. Please use “orderby”, not “order_by”.

    Thread Starter candell

    (@candell)

    OMG, I didn’t notice either. Thanks, that is working ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘get_users multiple meta keys’ is closed to new replies.