2

I am trying to set up a generic shortcode in WordPress with parameters for user meta key, meta value and roles that will count the total number of profiles that include certain values. So far I have made it work with one single role with the following code and shortcode:

[profilecounts metakey="Market" metavalue="149" userrole="franchisee_nm"]

add_shortcode('profilecounts', 'profile_counts');
function profile_counts($atts) { 
     $metakey = shortcode_atts( array('metakey' => 'None'), $atts );
     $metavalue = shortcode_atts( array('metavalue' => 'None'), $atts );
     $userrole = shortcode_atts( array('userrole' => 'avail_roles'), $atts );
return count( get_users(array('meta_key' => $metakey['metakey'], 'meta_value' => $metavalue['metavalue'], 'role' => $userrole['userrole'], ) ) );
} 

My issue is that I can't get it to work with multiple roles and/or make it default to all roles.

I want to use the following to count multiple roles (or something of the sort):

[profilecounts metakey="Market" metavalue="149" userrole="franchisee_nm,franchisee_co"]

And I want to use the following to count all roles (or something of the sort):

[profilecounts metakey="Market" metavalue="149"]

EDIT: Okay, I figured a way around doing multiple roles using a second shortcode without the role parameter for now, but if someone can help me do what I'm trying to do, that would be great. Thanks in advance.

[profilecounts_conm metakey="Market" metavalue="149"]

add_shortcode('profilecounts_conm', 'all_profile_counts');
function all_profile_counts($atts) { 
     $metakey = shortcode_atts( array('metakey' => 'None'), $atts );
     $metavalue = shortcode_atts( array('metavalue' => 'None'), $atts );
     $franchisees_co = count( get_users(array('meta_key' => $metakey['metakey'], 'meta_value' => $metavalue['metavalue'], 'role' => 'franchisee_nm', ) ) );
     $franchisees_nm = count( get_users(array('meta_key' => $metakey['metakey'], 'meta_value' => $metavalue['metavalue'], 'role' => 'franchisee_co', ) ) );
return round($franchisees_co+$franchisees_nm);
} 
2

Try this:

function profile_counts($atts) {
    $atts = shortcode_atts( array(
        'metakey'   => '',
        'metavalue' => '',
        'userrole'  => '',
    ), $atts );

    $user_ids = get_users( array(
        'meta_key'    => $atts['metakey'],
        'meta_value'  => $atts['metavalue'],
        'role__in'    => wp_parse_list( $atts['userrole'] ),
        'fields'      => 'ID',  // retrieve just the IDs
        'count_total' => false, // no need for FOUND_ROWS()
    ) );

    return count( $user_ids );
}

Important points:

  • For multiple roles, you can use the role__in parameter.

  • You only need to call shortcode_atts() once.

  • PS: role__in supports both single and multiple roles, and default to none (which means all roles). – Sally CJ May 8 at 20:25
  • This is perfect. Thank you! – John Smithly May 8 at 20:53
  • You're welcome. :) – Sally CJ May 8 at 23:05

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.