0

So I'm making a customer service portal using Wordpress, and it's been pretty good so far. To preface, I have a few relevant custom fields.

Customers have "isSalesperson" == No. Salespeople have "isSalesperson" == Yes. Then a salesperson has a "salespersonID" equivalent to a number. All of their customers have a salespersonID equivalent to that same number. There's also "super" which means you can see all customers. I want to only list the clients that have the same salespersonID as the salesperson when they are looking at this page.

$admin = wp_get_current_user();
$salespersonID = $admin->salespersonID;

if($salespersonID != "Super"){  
    $meta_query_args = array(
        'relation' => 'AND',
        array(
            'key'     => 'salespersonID',
            'value'   => $salespersonID,
        )
    );
}
else
{
    $meta_query_args = array(
        'relation' => 'AND',
        array(
            'key'     => 'salespersonID',
            'value'   => 0,
            'compare' => '!=',
        )
    );
}  

$meta_query = new WP_Meta_Query( $meta_query_args );
$args = array(
    'meta_key'     => 'companyname',
    'orderby'      => 'meta_value',
    'order'        => 'ASC',
    'meta_query'   => $meta_query,
);

$users = get_users($args);
foreach ( $users as $user ) {
  if($user->isSalesperson == "No"){
    echo '<option>' . $user->companyname . '</option>';
  }
}

My issue is that I'm not sure how to use the WP_Meta_Query in order to select only the users that meet the criteria of their salespersonID being equivalent to the current logged-in user's salespersonID. Currently, the code runs without error but doesn't actually do what I want. It simply shows all of the results.

2
  • Why not use custom roles for your salesperson instead of that meta field hack?
    – s_ha_dum
    Oct 7, 2015 at 15:51
  • Wouldn't that still give me a similar issue? I've already implemented it this way but for future role types I'll certainly use a custom role.
    – AKor
    Oct 7, 2015 at 16:10

0

Your Answer

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

Browse other questions tagged or ask your own question.