Support Forums

The forums are closed and will be removed when we launch our new site.

Looking for support? You can access the support system via your account.

Forums Forums Search & Filter Pro Get label instead of value using CMB2 to add meta boxes

Viewing 4 posts - 1 through 4 (of 4 total)
  • Lisa Kriga
    #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 Kriga

    Trevor Moderator
    #181794

    Search & 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 Moderator
    #181806

    Hi

    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.

    Lisa Kriga
    #181815

    Ok, I will try it that way! Thank you!

    // Lisa

Viewing 4 posts - 1 through 4 (of 4 total)

The forum ‘Search & Filter Pro’ is closed to new topics and replies.