1

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

1

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
    ),
);
  • works great.. syntax just eneds a ){ in line 2 – Rizzo Sep 6 '13 at 13:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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