callback: in Select Advanced not working?

Support MB Builder callback: in Select Advanced not working?

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #47990
    Roel PetraRoel Petra
    Participant

    Hi there,
    I'm working on a new site that allows you to track game progress.

    The site is set up like so:
    I have 2 custom post types: Games (slug: game) and Collectibles (slug: collectible)

    Game has the following custom fields (Field group ID: cf_game)
    Group: Collectible types (ID: gameCollectibleTypes (clonable, which makes this a repeater field))
    * Text: Collectible type (ID: gameCollectibleTypes_Name)

    Collectible has the following custom fields (Field group ID: collectible_cf)
    Text: Collectible name (ID: collectibleName (also use as post title before saving, using the rwmb_before_save_post action))
    Select advanced: Collectible type (ID: collectibleType)

    I want to use the values in the gameCollectibleTypes repeater in the collectibleType field.

    There's a relationship between the game and the collectible named: Game - Collectible (ID: game-collectible)
    This contains the game the collectible is linked to.

    Claude wrote me the following code to use for the callback function so that the values fill dynamically:

    add_filter('collectibleType_options', 'populate_collectibleType_options');
    
    function populate_collectibleType_options($collectibleOptions) {
        // Check if we're editing a collectible
        if (!isset($_GET['post'])) {
            return $collectibleOptions;
        }
    
        $collectibleId = intval($_GET['post']);
        
        // Get the related game using the relationship field
        $gameIds = rwmb_get_value('game-collectible', '', $collectibleId);
        
        if (empty($gameIds)) {
            return $collectibleOptions;
        }
        
        // Use the first related game (assuming a collectible is related to only one game)
        $gameId = is_array($gameIds) ? $gameIds[0] : $gameIds;
        
        // Get collectible types from the game
        $collectibleTypes = rwmb_the_value('gameCollectibleTypes', '', $gameId); // I've also tried rwmb_get_value.
        
        if (empty($collectibleTypes)) {
            return $collectibleOptions;
        }
        
        // Build options array from the collectible types
        $dynamicCollectibleOptions = [];
        foreach ($collectibleTypes as $collectibleType) {
            $collectibleName = isset($collectibleType['gameCollectibleTypes_Name']) ? $collectibleType['gameCollectibleTypes_Name'] : '';
            if (!empty($collectibleName)) {
                $dynamicCollectibleOptions[$collectibleName] = $collectibleName;
            }
        }
        
        return $dynamicCollectibleOptions;
    }
    #47991
    Roel PetraRoel Petra
    Participant

    Follow up:
    Whenever i use "callback: populate_collectibleType_options" or "callback: collectibleType_options". My site just crashes and I have to go into recovery mode.

    I have no clue why this is happening.

    #48002
    PeterPeter
    Moderator

    Hello,

    The suggestion from AI is not always correct. You can follow the topic below to know how to use a callback function for the select field
    https://support.metabox.io/topic/custom-field-select-is-not-saving-or-displaying-value/#post-44373

    A callable function that returns an array of choices. The function should return an array of 'value' => 'Label'.

    Refer to the documentation https://docs.metabox.io/fields/select/

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