0

I have a custom user field in the standard database, called company. I want to output on to a page the usermeta info only of users whose company matches Widgets Inc ...

How do I do that?

What I'd really like to be able to do is avoid putting this in a page template and instead create a shortcode to list user info, with functionality to limit by field attribute - ie. {listusers company="Widgets Inc"}

But I don't know how to do that.

1 Answer 1

0

To fetch users with certain meta field, you can use WP_User_Query class. Check codex manual for it.

So you can create a custom shortcode like this:

add_shortcode( 'listusers', 'wpse8170_listusers' );
function wpse8170_listusers( $atts, $content = '' ) {
    $atts = shortcode_atts( array( 'company' => false ), $atts );

    if ( empty( $atts['company'] ) ) return $content;

    $query = new WP_User_Query( array(
        'meta_key' => 'company', 
        'meta_value' => $atts['company'],
    ) );

    ob_start();

    // render your users list here

    return ob_get_clean();
}
2
  • Thanks Eugene. But that produces no output on my end. Should I be inserting something beneath "// render your users list here"? Oct 11, 2013 at 15:08
  • yes, echo your HTML code there Oct 11, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.