Forums › Forums › Search & Filter Pro › Get label instead of value using CMB2 to add meta boxes
Tagged: cmb2, custom meta box, label
- This topic has 3 replies, 2 voices, and was last updated 6 years, 4 months ago by Anonymous.
-
Anonymous(Private) July 3, 2018 at 7:36 am #181791
Hi,
I have used CMB2 to add meta boxes to my post types in my own theme, and I only get the value from the meta boxes in the search and filter form. How do I get the labels? Is the only alternative to add the options manually? I have only found (private) answers to how this works when using ACF.
Here is an example of how a select meta box is added:
$product->add_field( array( 'id' => 'id_here', 'name' => esc_html__( 'Example', 'text-domain' ), 'type' => 'select', 'default' => '0', 'options_cb' => 'example_options' ) ); function example_options() { return array( '0' => esc_html__( 'None', 'text-domain' ), '1' => esc_html__( 'One', 'text-domain' ), '2' => esc_html__( 'Two', 'text-domain' ), ); }
Kind regards,
Lisa KrigaTrevor(Private) July 3, 2018 at 11:12 am #181794Search & Filter fetches the value stored in the database, and not the label. A standard WordPress custom field stores just the one value (not a data value and paired label). The exception to this is if you use ACF to add meta boxes with custom fields to the post. We have added specific support for this popular free plugin so that it fetches the label.
With Post Meta fields on the form, you can manually add the value/label pairs yourself. I suspect you could also do it via PHP in the functions.php file, but I do not have a code example to show you.
Trevor(Private) July 3, 2018 at 11:46 am #181806Hi
Or you could use the Filter Input Object filter and change the values that way (the code runs in your functions.php file).
I suspect that you could use PHP to fetch the labels and then change each value that way.
-
AuthorPosts