WordPress.org

Support

Support » Plugins and Hacks » [Resolved] query through custom meta key and value

[Resolved] query through custom meta key and value

  • 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/

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author President McCheese
    Member

    @jason-lau

    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.

    Small fix –

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

    Thank you!

    Plugin Author President McCheese
    Member

    @jason-lau

    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.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] query through custom meta key and value’ is closed to new replies.