Custom Fields
Re-Envisioned
Support General Using rwmb_filter_id_value for cloned grouped sub fields

  • Creator
    Topic
  • #27754
    Resolved BucketPress
    Participant

    Hi I have the following group sub fields set up:
    group_id
    - subfield_1
    - subfield_2
    - subfield_3

    The group can be cloned.

    Doing a test to see if this works by using the following code, but it doesn't seem to work:

    add_filter('rwmb_subfield_3_value', function($new, $field, $old) {
       return 'Test Value';
    }, 10, 3);

    Secondly, since the group can be cloned, if I am trying to set the value of subfield_3 based on the value of subfield_2, how can I target/get the value of each cloned group's subfield_2 and set the corresponding subfield_3 value in the above filter?

    If this were simple fields that cannot be cloned, then we could easily do something like:

    if ( isset($_POST['field_2']) ) {
       ...do something to field_3 with field_2 value
    }

    But how do we target subfields for a group that can be cloned?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Author
    Replies
  • #27772
    Long Nguyen
    Moderator

    Hi,

    The group saves the group ID as meta key in the database so if you want to modify the subfield value, you need to use the filter of group ID. For example:

    add_filter( "rwmb_group_id_value", function( $new, $field, $old ) {
        $new['subfield_3'] = 'Test value';
        return $new;
    }, 99, 3 );

    Get more information about the group value here https://docs.metabox.io/extensions/meta-box-group/#getting-sub-field-values

    #27775
    BucketPress
    Participant

    Hi Long Nguyen,

    Thanks, makes perfect sense to filter the parent Group_ID instead, and loop through the $new array to change the values of the cloned group and subfields.

    Have tried and it works.

    However, when I update a post, the 'changes' of values made through the filter does not show immediately. I have to refresh the post edit page again before the changes are shown.

    This makes the user think that changes have not been made upon clicking 'update' of post.

    #27800
    Long Nguyen
    Moderator

    Hi,

    Yes, it the default behavior of Block editor when we use the PHP code to do something. The data was modified but it does not show until refreshing the page. You can try to use the Classic editor, after clicking update, the page also reloads.

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