Take values from database in a select

Support General Take values from database in a selectResolved

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #13798
    proyectohappyweb@gmail.com[email protected]
    Participant

    Hello,

    I'm using ACF but I change to your plugin.
    In a select, I want to select the groups of buddypress.
    In ACF I do this to get the groups:

    function dynamic_grupo_dropdown ( $field ){
    
    global $wpdb;
    $table = $wpdb->prefix."bp_groups";
    $grupos = $wpdb->get_results( "SELECT id,name FROM $table" );
    
    if(!empty($grupos)){
        foreach($grupos as $grupo){
            $field['choices'][$grupo->id] = $grupo->name;
        }
    }
    
    return $field;
    }
    add_filter('acf/load_field/key=key_post_grupos_acceso', 'dynamic_grupo_dropdown');

    But don't know how can do this with yours plugin...

    Can you help me?

    Thanks.

    #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;
    }
    #13822
    proyectohappyweb@gmail.com[email protected]
    Participant

    ohh perfect!!! It run fantastic.

    The only thing is:

    this code

    if ( !empty( $groups ) ) {
        return [];
    }

    is

    if ( empty( $groups ) ) {
        return [];
    }

    Many thanks with the quickly response!!
    Greetings.

Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.