WPIntell

Source evidence

Value of Select and Radio Elements are not saved to db

CMB2 · support · 2024-04-14T20:40:00+00:00

complaintsentiment
mediumseverity
0.93relevance
3replies
Evidence linked to opportunitycommercial context

Proof Health

Open evidence

Commercial opportunities need traceable source links before they are treated as build-worthy.

4 / 34 rows with source links

11.8% of this page's analysis has direct source links.

0 build-decision rows missing links

0 rows here require auditable proof before promotion.

30 rows with no attached evidence

0 rows have source counts but still need direct links.

Conversation

support
janole unresolved
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! 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? 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? 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.

Comments

3 shown
Michael Beckwith 2024-04-14T21:13:00+00:00

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?

janole 2024-04-15T09:09:00+00:00

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?

Michael Beckwith 2024-04-15T12:57:00+00:00

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.