skip to Main Content

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

enter image description here

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


  1. Chosen as BEST ANSWER

    Solved. The problem was in Uppercase of key [wpusers Department="IEDK"]. I changed to small "d" everywhere and now it works..........


  2. Remove extract and then try to debug with uncomment lines

    function wpusers_shortcode( $atts ) {
    
        $ar = shortcode_atts( 
           array('Department' => ''),
            $atts
        );
    
    // print_r($ar); die;
    
    
    $args = array(  
    'meta_query' => array(
    'relation' => 'AND',
        array(
        'key' => 'Department',
        'value' => $ar['Department'],
            ),
        )
    );
    
     // print_r($args); die;
    
    $user_query = new WP_user_Query( $args );
    
    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’ );

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search