autofill a select custom field with a custom field from another group.
- This topic has 5 replies, 2 voices, and was last updated 3 years ago by
Long Nguyen.
-
AuthorPosts
-
April 26, 2022 at 2:09 AM #35789
Fernando Arbex
ParticipantHello everyone,
How to use a value from a custom field (with id: leader) of a group and use this leader as a select field in another group?
In this scenario I'm building, there will be several groups that will first be created by the leader filling the group form.
Then the members of that leader group will fill the member form and chose the leader. This leader will be the one that was filled when they create the group and I want it to be auto-populated whenever a new group is created.
Any idea?
April 26, 2022 at 9:59 PM #35797Long Nguyen
ModeratorHi Fernando,
This case is beyond the scope of support of Meta Box. Meta Box does not support populating a field value based on another field value. You can use JS code with AJAX to update the select options.
April 26, 2022 at 11:59 PM #35803Fernando Arbex
ParticipantHi Long,
You mean, create a function in Code Snipets called function_name (for example), and call it in the select field?
The select field has this message below:
To use a PHP function that returns an array of options, enter callback: function_name.
The callback function must be declared before adding to the box.So I would enter callback: function_name?
Is that it?
April 28, 2022 at 12:17 PM #35811Long Nguyen
ModeratorHi,
Yes, you can use the callback function to create dynamic options for the select field. But the options will be parsed on load, not "live" (realtime update) as JS does.
April 29, 2022 at 1:26 AM #35820Fernando Arbex
ParticipantHi long,
Thank you for your answer, I'm trying to make it work but need a hand.
Althought I have the direction I'm quite stucked on how to make it work.
I found a few codes on the internet that does that and I modified for my scenario.
Here is the code.<?php
function autoPopulateLeader( $field ) {
// reset lider_membro
$field['lider_membro'] = array();
// get the value of post_title (leader) from Custom Group with id
// informacoes_celula without any formatting
$choices = rwmb_get_value( 'post_title', 'informacoes_celula' );
// explode the value so that each line is a new array piece
$choices = explode("\n", $choices);
// remove any unwanted white space
$choices = array_map('trim', $choices);
// loop through array and add to field 'lider_membro'
if( is_array($choices) ) {
foreach( $choices as $choice ) {
$field['lider_membro'][ $choice ] = $choice;
}
}
// return the field
return $field;
}Notes:
- lider_membro is custom field (group ID membro) that I want to be populated from the post_title of the group ID informacoes_celula
- post_title is the leader custom field in the group ID informacoes_celula
- this code is in code snipet to load on all pages
- in the custom field type for lider_membro I used Select and callback: autoPopulateLeaderApril 29, 2022 at 12:36 PM #35825Long Nguyen
ModeratorHi,
There are two notes in this case:
1. The callback function should return an array of values and labels as setting
options
function my_custom_options() { $options = [ 'java' => 'Java', 'javascript' => 'JavaScript', 'php' => 'PHP', ]; return $options; }
register the field select
[ 'name' => 'Select', 'id' => 'select', 'type' => 'select', 'multiple' => true, 'placeholder' => 'Select an item', 'select_all_none' => true, 'options' => my_custom_options(), ],
2. The helper function
rwmb_meta()
runs after the filter hookrwmb_meta_boxes
so it does not work with the callback function. You can use the functionget_post_meta()
to get field value inside the function callback.Refer to this topic https://support.metabox.io/topic/bug-settings-not-available-for-custom-fields
-
AuthorPosts
- You must be logged in to reply to this topic.