select function callback

Support MB Builder select function callback

  • This topic has 3 replies, 2 voices, and was last updated 2 days ago by PeterPeter.
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #48760
    369cycle369cycle
    Participant

    I create a select function that I created earlier.

    <?php
    $groups = rwmb_meta('group_pictag');
    foreach ( $groups as $group ) {
    
    // Field tw_tag:
    echo $group[ 'tw_tag' ] ?? '';
    
    // Field en_tw:
    echo $group[ 'en_tw' ] ?? '';
    
    }
    ?>

    Function syntax

    function getGroupDetails() {
    $groups = rwmb_meta('group_pictag');
    $result = [];
    
    foreach ($groups as $group) {
    // Add tw_tag and en_tw to the result array
    $result[] = [
    $group['en_tw'] => $group['tw_tag'], // Ensure the key name is valid
    ];
    }
    
    return $result;
    }

    Write the selector

    callback: getGroupDetails

    And call the custom sanitize callback

    Fill in getGroupDetails

    What's wrong?

    #48772
    PeterPeter
    Moderator

    Hello,

    The callback function for the select field must return an array of keys and values. Refer to this topic:
    https://support.metabox.io/topic/custom-field-select-is-not-saving-or-displaying-value/#post-41725

    Also, the helper function rwmb_meta() doesn't work at the init hook when registering the custom fields. You can use the WordPress function get_post_meta() to get the field value (post meta) in the callback function.

    #48776
    369cycle369cycle
    Participant
    <?php
    $groups = rwmb_meta('group_pictag');
    foreach ( $groups as $group ) {
    
    // Field tw_tag:
    echo $group[ 'tw_tag' ] ?? '';
    
    // Field en_tw:
    echo $group[ 'en_tw' ] ?? '';
    
    }
    ?>

    How do I change the above to get_post_meta()?

    #48783
    PeterPeter
    Moderator

    Hello,

    Here is an example $groups = get_post_meta( $post_id, 'field_id' );
    following the documentation https://developer.wordpress.org/reference/functions/get_post_meta/

    Please note that we don't support the customization code. If you are not able to complete the task, we offer a customization service with an extra fee. Please contact us here for more details https://metabox.io/contact/

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