WordPress.org

Forums

User Meta Manager
[resolved] query through custom meta key and value (4 posts)

  1. chryaner
    Member
    Posted 1 year ago #

    i was trying to do it like that, and it didnt worked out

    get_users(array('meta_key' => 'Department', 'meta_value' => 'finance')

    there are 11 users with different departments. I created a new meta, that is a Select Menu - with multiple selection turned on.

    how do i query through all the users with a specific department?

    thanks
    Iliya

    https://wordpress.org/plugins/user-meta-manager/

  2. President McCheese
    Member
    Plugin Author

    Posted 1 year ago #

    You can try this. I haven't tested it, but it should do the trick.

    global $wpdb;
    $query = "SELECT usermeta.*, users.* FROM " . $wpdb->usermeta . " usermeta, " . $wpdb->users . " users WHERE users.ID=usermeta.user_id and usermeta.meta_key='department'";
    $data = $wpdb->get_results($query);
    
    foreach($data as $_user):
     $department = $_user->meta_value;
     if(is_array($department) && in_array('finance', $department))
     print($_user->ID . ', ' . $_user->user_email . '<br />');
    endforeach;

    I join the users and usermeta tables in my query.

  3. chryaner
    Member
    Posted 1 year ago #

    Small fix -

    $department = unserialize($_user->meta_value);

    Thank you!

  4. President McCheese
    Member
    Plugin Author

    Posted 1 year ago #

    I knew I was forgetting something!

    Just thought I would mention the User Meta Manager query shortcode -

    [umm query before_result="<section class='umm-query-result'>" after_result="</section>" before_item="<ul class='umm-query-user'>" after_item="</ul>" item="<li class='umm-query-item'><strong>%k</strong>: %v</li>" key_format="ucwords" value_format="ucfirst" list="user_nicename, meta_value" where="usermeta.meta_key='department' AND usermeta.meta_value LIKE '%finance%'"]

    It will basically achieve the same result as the query I posted.

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • User Meta Manager
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic

Tags

No tags yet.