• Hey, love the plugin and been using it forever to make my life easier. I usually only need it for text fields or checkboxes, but with some recent development I need to use the select drop down box.

    I realised after a lot of troublehooting, that values of the select (or radio) elements are not being saved to the database, making them return blank when called in the loop. They do not show up when trying to var_dump in the loop, unlike all the other custom fields from CMB2 that show up just fine. Only when I manually update the select field using update_post_meta, it updates the value and shows up.

    This is my setup in functions.php:

    add_action( 'cmb2_admin_init', 'cmb2_retreats_metaboxes' );
    
    function cmb2_retreats_metaboxes() {
    
    	$prefix = '_retreats_';
    
    	$cmb_retreat = new_cmb2_box( array(
    		'id'            => 'lesson_meta_retreat-info',
    		'title'         => __( 'xxxxx', 'cmb2' ),
    		'object_types'  => array( 'retreats', ), // Post type
    		'context'       => 'normal',
    		'priority'      => 'high',
    		'show_names'    => true, // Show field names on the left
    	) );
    
    	$cmb_retreat->add_field( array(
    		'name'             => 'Teaser Frame',
    		'desc'             => 'xxxxxx',
    		'id'               => $prefix . 'arch-type',
    		'type'             => 'select',
    		'show_option_none' => false,
    		'default'          => 'arch1',
    		'options'          => array(
    			'arch1' => 'one',
    			'arch2' => 'two',
    			'arch3' => 'three',
    			'arch4' => 'four',
    			'arch5' => 'five',
    			'arch6' => 'six',
    		),
    	) );
    
    }

    This is a basic setup in the loop im using right now for testing:

    <?php if (have_posts()) : ?>
    	<?php while (have_posts()) : the_post();
    
    $arch_type = get_post_meta( get_the_ID(), '_retreats_arch-type', true );
    
    if ( 'arch1' === $arch_type ) {
    		echo 'it works';
    	} else {
    		// some other PHP code
    	}
    <?php endwhile; ?>
    	<?php else : ?>
    		<article class="nothing"></article>		
    <?php endif; ?>

    Can someone help me out figuring out what might be going wrong? Oh I already disabled all plugins to check for any interference.

    Greetings!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    Have you verified, via a database connection that things save, via UI only? Were values showing back up in the CMB2 UI after saving and refreshing the page?

    Thread Starter janole

    (@janole)

    indeed the UI in the wp-admin post editor keeps the changes after reload. I assumed it was a cache thing related to WordPress, since the changes never make it to the front end. Could this be another issue?

    Plugin Contributor Michael Beckwith

    (@tw2113)

    The BenchPresser

    No caching issues that I can think of when it comes to get_post_meta() so the only other idea I have is that the meta keys aren’t matching up like what’s saved in the wp_postmeta table or things are being tested on a wrong post, where the one being viewed doesn’t have meta saved with these keys yet.

    That said, based on the code provided, it **should** be finding something if the post in question has had the fields saved. CMB2 itself is more a library meant to create the UI/etc for the fields, but shouldn’t be somehow interfering with the fetching of the saved data.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Value of Select and Radio Elements are not saved to db’ is closed to new replies.