Get choices of a select field

Support MB Builder Get choices of a select fieldResolved

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #42721
    ReisecouponReisecoupon
    Participant

    Hi,

    how can I get the choices of a select field? For example, I have a list (id: mylist) and 3 choices (red, blue, yellow) and I want to list them on the front end.

    Thank you for your help!

    #42725
    PeterPeter
    Moderator

    Hello,

    If you want to output the selected choices, please follow the documentation https://docs.metabox.io/fields/select/#template-usage

    If you want to output all supported choices, please use the function rwmb_get_field_settings(). Refer to the documentation https://docs.metabox.io/functions/rwmb-get-field-settings/

    #42789
    ReisecouponReisecoupon
    Participant

    Hi,

    I have the custom post type "listing", related to this custom post type I try to use this code to show all available choices of my select custom field "city":

    <?php
    $field = rwmb_get_field_settings( 'city' );
    echo ($field);
    ?>

    Nothing happens.

    What did I wrong?

    Thank you for your help!

    #42791
    PeterPeter
    Moderator

    Hello,

    You should add the code to the template of the listing post to get the current post ID. If not you need to pass the post ID to the function. And the return value is an array so you need to use the function var_dump() or print_r() to print out the value.

    For example:

    $field = rwmb_get_field_settings( 'city', '', 123 ); // 123 is the post ID
    echo '<pre>';
    print_r( $field );
    echo '</pre>';
    #42793
    ReisecouponReisecoupon
    Participant

    Thank you, but I would like to show (echo) all available choices of this field "city", not the selected choice of a random related custom post.

    For example, my field "city" has 3 choices:
    - Barcelona
    - Madrid
    - London

    And I simple want to echo these fields.

    Thank you!

    #42797
    PeterPeter
    Moderator

    Hello,

    Use the function print_r() to know the data structure of the field settings. You will need to have a basic knowledge of coding to echo the choice. For example:

    $field = rwmb_get_field_settings( 'city', '', 123 ); // 123 is the post ID
    $field_choices = $field['options'];
    
    foreach ($field_choices as $value => $label) {
    	echo "Value: " . $value;
    	echo "<br />";
    	echo "Label: " . $label;
    	echo "<br />";
    }

    If you are not able to complete the task, please contact us here https://metabox.io/contact/
    our development team will help you with an extra fee.

    #42802
    ReisecouponReisecoupon
    Participant

    Hi, thanks, it works, but what happens if I delete this post? The code will not work.

    The problem is that the post isn´t a permanent content on the site, it has an expiration and after that I delete it.

    I tried to use the field group id (because the field group is permanent, I wouldn´t delete it), but it does not work woth it.

    Isn´t any better solution?

    Thank you!

Viewing 7 posts - 1 through 7 (of 7 total)
  • You must be logged in to reply to this topic.