Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm using human made's custom meta boxes framework (https://github.com/humanmade/Custom-Meta-Boxes). I want to add a select drop down in my metabox wich is populated from the users with multiple roles on my WP site. Referencing this post : Get multiple roles with get_users

I came up with:

add_filter( 'cmb_meta_boxes', 'users_metabox' );

function eusers_metabox( array $meta_boxes ) {

    $prefix = 'user_';

    global $wpdb;
    $blog_id = get_current_blog_id();

    $user_query = new WP_User_Query( array(
        'meta_query' => array(
            'relation' => 'OR',
            array(
                'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
                'value' => 'Administrator',
                'compare' => 'like'
              ),
            array(
                'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
                'value' => 'Editor',
                'compare' => 'like'
              )
          )
      ) 
    );

    $fields = array(
    array( 
            'id'   => $prefix . 'user_sub', 
            'name' => 'Subscriber User', 
            'type'     => 'select',
            'use_ajax' => false,
            'options'  => $user_query,  // this is where you populate the select in metabox
        ),
    );

    $meta_boxes[] = array(
        'title' => 'Location Info',
        'pages' => 'em_users',
        'context'    => 'normal',
        'priority'   => 'high',
        'fields' => $fields
    );

    return $meta_boxes; 

}

Works somewhat, but seems to return just capital letters. Any ideas?

Need to output (UserID) as variable constant

share|improve this question

1 Answer 1

up vote 0 down vote accepted

I think the problem is you are passing the WP_User_Query object, not the results of the query. Try to change:

$fields = array(
array( 
        'id'   => $prefix . 'user_sub', 
        'name' => 'Subscriber User', 
        'type'     => 'select',
        'use_ajax' => false,
        'options'  => $user_query,  // this is where you populate the select in metabox
    ),
);

To:

$users_ids = array();
if ( !empty( $user_query->results )){
    foreach($user_query->results as $user){
        $users_ids[] = $user->ID;
    }
}

$fields = array(
array( 
        'id'   => $prefix . 'user_sub', 
        'name' => 'Subscriber User', 
        'type'     => 'select',
        'use_ajax' => false,
        'options'  => $users_ids,  // this is where you populate the select in metabox
    ),
);
share|improve this answer
    
works great.. syntax just eneds a ){ in line 2 –  Rizzo Sep 6 '13 at 13:53

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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