• Resolved daco.marseille

    (@dacomarseille)


    Hi,
    first of all thanks so much for your plugin!
    I love it!

    I have an issue which I don’t seem to understand,
    I need to build an option array for a multi checkbox,
    this array is made out of a wp_query

    Here is the code:

    /*
    * initie metaboxes
    */
    
    add_action( 'cmb2_init', 'cmb2_ratchet_metaboxes' );
    /**
     * Define the metabox and field configurations.
     */
    function cmb2_ratchet_metaboxes() {
    
        // Start with an underscore to hide fields from custom fields list
        $prefix = '_cmb_';
    
    /**
         * Initiate the metabox for Works
         */
        $cmb = new_cmb2_box( array(
            'id'            => 'works_details',
            'title'         => __( 'Work Details', 'cmb2' ),
            'object_types'  => array( 'work', ), // Post type
            'context'       => 'normal',
            'priority'      => 'high',
            'show_names'    => true, // Show field names on the left
            // 'cmb_styles' => false, // false to disable the CMB stylesheet
            // 'closed'     => true, // Keep the metabox closed by default
        ) );
    
    	$cmb->add_field( array(
            'name'       => __( 'Work main image', 'cmb2' ),
            'desc'       => __( 'Work main image, dimensions: 1920px x 380px', 'cmb2' ),
            'id'         => $prefix . 'work_main_img',
            'type' => 'file',
        ) );
    
    	/*custom query to build multicheckbox options*/
    	$clientsArray = array();
    	$subs = new WP_Query( array(  'post_type' => 'client', 'orderby' => 'date','posts_per_page'=>'-1' ));
    	if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post();
    	$postid = $post->ID;
    	$title = get_the_title( $postid );
    	$clientsArray[$postid] = $title;
    	endwhile;
    	endif; wp_reset_postdata();
    
    	$cmb->add_field( array(
    		'name'    => __( 'Clients', 'cmb2' ),
    		'desc'    => __( 'check clients (optional)', 'cmb2' ),
    		'id'      => $prefix . 'multicheckbox_clients',
    		'type'    => 'multicheck',
    		//'multiple' => true, // Store values in individual rows
    		'options' => $clientsArray,
    		'inline'  => true, // Toggles display to inline
    	) );
    }

    The problem is at the very end,
    $clientsArray is not recognised,
    when I print_r it it gives:
    Array ( [27] => Argos [26] => Google )

    When I switch and enter manually the options:

    'options' => Array ( 27 => 'Argos', 26 => 'Google' ),

    It works like a charm,

    I don’t know what I’m missing,
    but if you could help that would be great!

    Thank you!

    https://wordpress.org/plugins/cmb2/

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

The topic ‘multicheck options question’ is closed to new replies.