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);
}
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);
}
Share
Improve this question
edited May 8, 2019 at 20:12
Michael
asked May 8, 2019 at 19:20
MichaelMichael
2811 silver badge14 bronze badges
1 Answer
Reset to default 2Try 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.