callback: in Select Advanced not working?
Support › MB Builder › callback: in Select Advanced not working?
- This topic has 2 replies, 2 voices, and was last updated 2 weeks, 2 days ago by
Peter.
-
AuthorPosts
-
April 3, 2025 at 9:50 PM #47990
Roel Petra
ParticipantHi 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; }
April 3, 2025 at 9:52 PM #47991Roel Petra
ParticipantFollow 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.
April 4, 2025 at 10:15 PM #48002Peter
ModeratorHello,
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-44373A 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/
-
AuthorPosts
- You must be logged in to reply to this topic.