I have such code with attribute extraction to use it in shortcode.
<?php
function wpusers_shortcode( $atts ) {
extract(shortcode_atts( array(
'Department' => '',
), $atts
)
);
$args = array(
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'Department',
'value' => $Department,
),
)
);
// The Query
$user_query = new WP_user_Query( $args );
// User Loop
if ( ! empty( $user_query->get_results() ) ) {
foreach ( $user_query->get_results() as $user ) {
echo '<p>' . $user->display_name . '</p>';
}
} else {
echo 'No users found.';
}
}
add_shortcode( 'wpusers', 'wpusers_shortcode' );
?>
The shortcode is [wpusers Department="IEDK"].
Users are assigned to custom field Department
But at front end I have a message – No Users Found.
When I add attr (IEDK) in the code I can see them
extract(shortcode_atts( array(
‘Department’ => ‘IEDK’,
or
'key' => 'Department',
'value' => 'IEDK',
Whhere can be a problem?
Thanks in advance.
2
Answers
Solved. The problem was in Uppercase of key [wpusers Department="IEDK"]. I changed to small "d" everywhere and now it works..........
Remove extract and then try to debug with uncomment lines
add_shortcode( ‘wpusers’, ‘wpusers_shortcode’ );