5

I'm trying to make a query for users using WP_User_Query() I need to filter users by role shop_manager and custom taxonomy called shop-category and current taxonomy term ID.

The code I have so far:

<?php 

// WP_User_Query arguments
$args = array (
    'role'           => 'shop_manager',
    'order'          => 'DESC',
    'orderby'        => 'user_registered',
    'tax_query' => array(
    array('taxonomy' => 
    'shop-category', 
    'field' => 'id', 
    'terms' => $term_id ) ) 
);

// The User Query
$user_query = new WP_User_Query( $args );

// The User Loop
if ( ! empty( $user_query->results ) ) {
    foreach ( $user_query->results as $user ) {
        echo '<li><span>' . esc_html( $user->shop_name ) . '</span></li>';
    }
} else {
    // no users found
}

?>

To get term ID I was using this code

<?php $queried_object = get_queried_object();
$term_id = $queried_object->term_id; 

echo 'ID = '. $term_id;?>

Now it's showing all users with shop_manager role. Looks like taxonomy doesn't work.

  • What plugin are you using for the shop? – Brad Dalton Aug 13 '15 at 11:03
  • I'm using WooCommerce plugin. – Erik Aug 13 '15 at 11:13
4

Apparently there is no core implementation of 'tax_query' in WP_User_Query yet.

Check the ticket here for more info --> https://core.trac.wordpress.org/ticket/31383

Nevertheless there is an alternative way using get_objects_in_term

$taxonomy = 'shop-category';
$users = get_objects_in_term( $term_id, $taxonomy );    

if(!empty($users)){

    // WP_User_Query arguments
    $args = array (
        'role'           => 'shop_manager',
        'order'          => 'DESC',
        'orderby'        => 'user_registered',
        'include'        => $users
    );

    // The User Query
    $user_query = new WP_User_Query( $args );

    // The User Loop
    if ( ! empty( $user_query->results ) ) {
        foreach ( $user_query->results as $user ) {
            echo '<li><span>' . esc_html( $user->shop_name ) . '</span></li>';
        }
    } 
    else {
        // no shop_manager found 
    }
}
else {
    // no users found
}
2

There is no tax_query parameter for the WP_User_Query class. Since you've tagged the question with user-meta I can only assume you store the ID (or even the term name itself?) of a particular shop-category term as a user meta entry.

In this case you would need something like this:

// The 'WP_User_Query' arguments array
$args = array(
    'role'          => 'shop_manager',
    'order'         => 'DESC',
    'orderby'       => 'user_registered',
    'meta_key'      => 'shop_name', // Is this the meta key you are using?
    'meta_value'    => 'the_term_name_or_term_id', // Based on however you store your meta data
    'meta_compare'  => '=',
);
  • Thanks for your response. I'm slowly getting there.Yes i'm using term ID. For Example: Taxonomy is "shop-category" Term name: gits, accessories, perfumery, etc... I was trying to pass the term ID in the query. Will try with meta_key now... – Erik Aug 13 '15 at 11:16
  • @Erik Please note that my initial code had a hyphen instead of an underline in 'shop_name'. You should pay attention to what you actually need, don't just copy / paste things in general. :) – webtoure Aug 13 '15 at 11:30
  • @Erik Did you managed to make it work? Note that you can try to set the meta_compare argument like so:'meta_compare' => 'like' in case you don't get the wanted results. – webtoure Aug 13 '15 at 18:10
  • If you use meta data then @webtoure's answer is fine. If you use taxonomies follow mine. – RafaSashi Nov 14 '16 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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