• Resolved reypm

    (@reypm)


    I am defining my metaboxes as follow:

    $meta_boxes[] = array(
            'id'       => 'related_link_1',
            'title'    => __('Noticia relacionada (1)'),
            'pages'    => array( 'post', 'page' ),
            'context'  => 'normal',
            'priority' => 'high',
            'fields' => array(
                array(
                    'name'  => 'Noticia relacionada',
                    'id'    => $prefix . 'related_link_1',
                    'type'  => 'select_advanced',
                    'clone' => true,
                ),
            )
        );

    And all works fine, metaboxes are displayed on post add|edit. Now I need to fill that metaboxes using all the post I have on my DB since this is a related new, how do I achieve this?

    https://ww.wp.xz.cn/plugins/meta-box/

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thread Starter reypm

    (@reypm)

    Ok, researching for a possible solution I’ve found this at demo.php file:

    array(
        'name'        => __( 'Select', 'meta-box' ),
        'id'          => "{$prefix}select_advanced",
        'type'        => 'select_advanced',
        // Array of 'value' => 'Label' pairs for select box
        'options'     => array(
            'value1' => __( 'Label1', 'meta-box' ),
            'value2' => __( 'Label2', 'meta-box' ),
        ),
        'multiple'    => false,
        'placeholder' => __( 'Select an Item', 'meta-box' ),
    )

    Now, how do I get all the post from the DB?

    Plugin Author Anh Tran

    (@rilwis)

    To get all post from DB, you can use this code:

    $posts = get_posts( array(
        'post_type' => array( 'post', 'page' ), // Add your post types here
        'posts_per_page' => -1,
        'orderby' => 'title',
        'order' => 'ASC',
    ) );
    $options = array();
    foreach ( $posts as $post )
    {
        $options[$post->ID] = $post->post_title;
    }

    Then you can pass $options to the field like this:

    array(
        'name'        => __( 'Select', 'meta-box' ),
        'id'          => "{$prefix}select_advanced",
        'type'        => 'select_advanced',
        // Array of 'value' => 'Label' pairs for select box
        'options'     => $options,
        'multiple'    => false,
        'placeholder' => __( 'Select an Item', 'meta-box' ),
    )
    Thread Starter reypm

    (@reypm)

    Hi, excellent but because I have a lot of post this option is not suitable for me since page get too slow which leads me to the next question, can I do the same using Select2 Ajax functionality? How?

    Plugin Author Anh Tran

    (@rilwis)

    Right now the plugin doesn’t support ajax functionality for Select2. But it’s put on the issue list on Github and we will try to implement it later.

    Thread Starter reypm

    (@reypm)

    Ohhh nice but later you mean today or other day? I need this as soon as you can, any ETA?

    Plugin Author Anh Tran

    (@rilwis)

    The time isn’t specified yet. I hope we can implement it in next major version update.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Set values for select_advanced field type’ is closed to new replies.