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);
}