Support General Take values from database in a select Reply To: Take values from database in a select

#13805
Anh TranAnh Tran
Keymaster

Hi, you can do like this with Meta Box:

First, separate the code that get list of BuddyPress groups into a function (it's a good practice to keep this logic away from plugins):

function prefix_get_bp_groups() {
    global $wpdb;
    $table = $wpdb->prefix."bp_groups";
    $groups = $wpdb->get_results( "SELECT id,name FROM $table" );

    if ( !empty( $groups ) ) {
        return [];
    }
    $return = [];
    foreach( $groups as $group ) {
        $return[$group->id] = $group->name;
    }
    return $return;
}

Then register a meta box with the following code:

add_filter( 'rwmb_meta_boxes', 'prefix_register_meta_boxes' );
function prefix_register_meta_boxes( $meta_boxes ) {
    $meta_boxes[] = [
        'title' => 'Your title',
        'fields' => [
            [
                'id' => 'your_key',
                'name' => 'BuddyPress group',
                'type' => 'select',
                'options' => prefix_get_bp_groups(), // THIS
            ],
        ],
    ];
    return $meta_boxes;
}