Custom Fields
Re-Envisioned
Support General Take values from database in a select

This topic contains 2 replies, has 2 voices, and was last updated by  proyectohappyweb@gmail.com 5 months, 4 weeks ago.

  • Creator
    Topic
  • #13798
    Resolved
    proyectohappyweb@gmail.com
    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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Author
    Replies
  • #13805

    Anh 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

    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 2 replies - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.