Field conditional logic doesn't work with Gutenberg
- This topic has 6 replies, 2 voices, and was last updated 5 years, 1 month ago by
Yumikom.
-
AuthorPosts
-
February 21, 2020 at 12:31 PM #18362
Yumikom
ParticipantHi,
I created a Post field and set the Conditional Logic in Advanced as below.
- Visible All
- post_type = page
However, when Gutenberg is enabled, conditional logic does not work.
This field has been displayed for all post types.add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' ); function your_prefix_register_meta_boxes( $meta_boxes ) { $prefix = ''; $meta_boxes[] = array ( 'title' => esc_html__( 'test', 'text-domain' ), 'id' => 'test', 'post_types' => array( 0 => 'post', 1 => 'general', ), 'context' => 'normal', 'priority' => 'high', 'fields' => array( array ( 'id' => $prefix . 'post_vw01o0dnsbe', 'type' => 'post', 'name' => esc_html__( 'Post', 'text-domain' ), 'post_type' => array( 0 => 'post', ), 'field_type' => 'select_advanced', 'visible' => array( 'when' => array( array ( 0 => 'post_type', 1 => '=', 2 => 'page', ), ), 'relation' => 'and', ), ), ), ); return $meta_boxes; }
February 22, 2020 at 11:52 AM #18374Anh Tran
KeymasterHi Yumikom,
I see you set a condition to show a field based on
post_type
. Unfortunately, the plugin doesn't support that kind of condition. You can change the visibility of a field or a meta box based on a value of another field or another input.February 24, 2020 at 9:24 AM #18379Yumikom
ParticipantHi,
I'm using Metabox for many years. A license is also bought every client.
This condition was used so far, and it was no problem. It can't be used in Gutenberg.
Please support post_type as a field condition even for posts using Gutenberg.
I'm having trouble getting Metabox in the classic editor I've used up to now to be able to migrate to Gutenberg.Regards,
YumikoFebruary 25, 2020 at 4:18 PM #18398Anh Tran
KeymasterHi Yumiko,
Thanks a lot for being our customer for years! I really appreciate that and your support!
Regarding this issue: a problem here is you set the meta box to appear to only 2 post types post and general. So, when you set the conditional logic for post type = page, it can't work (because the meta box is not loaded for that post type). Can you please change the
post_types
parameter of the meta box to includepage
and try again? I've checked and seen that it supportspost_type
rule.February 25, 2020 at 9:12 PM #18408Yumikom
ParticipantHi Anh,
I'm always thankful for your support.
I was relieved to support post_type rule.
I use this condition a lot.As you suggested, I tried with simple settings.
But again, the condition is not working in Gutenberg, which is fine in the classic editor.With the following settings, I want this field to be displayed only when post_type is 'page', but it will be displayed in 'post', 'page' and 'general'.
add_filter( 'rwmb_meta_boxes', 'your_prefix_register_meta_boxes' ); function your_prefix_register_meta_boxes( $meta_boxes ) { $prefix = ''; $meta_boxes[] = array ( 'title' => esc_html__( 'test', 'text-domain' ), 'id' => 'test', 'post_types' => array( 0 => 'post', 1 => 'page', 2 => 'general', ), 'context' => 'normal', 'priority' => 'high', 'fields' => array( array ( 'id' => $prefix . 'post_mnfi92e2ko', 'type' => 'post', 'name' => esc_html__( 'Post', 'text-domain' ), 'post_type' => array( 0 => 'page', ), 'field_type' => 'select_advanced', 'visible' => array( 'when' => array( array ( 0 => 'post_type', 1 => '=', 2 => 'page', ), ), 'relation' => 'and', ), ), ), ); return $meta_boxes; }
Thanks and Regards,
YumikoFebruary 29, 2020 at 12:01 AM #18432Anh Tran
KeymasterHi Yumiko,
I found the problem. The
post_type
is not a Meta Box field, so using it the normal way doesn't work. You need to use outside conditions.Change your code to:
add_filter( 'rwmb_meta_boxes', function ( $meta_boxes ) { $prefix = ''; $meta_boxes[] = array ( 'title' => esc_html__( 'test', 'text-domain' ), 'id' => 'test', 'post_types' => array( 0 => 'post', 1 => 'page', 2 => 'general', ), 'context' => 'normal', 'priority' => 'high', 'fields' => array( array ( 'id' => $prefix . 'post_mnfi92e2ko', 'type' => 'post', 'name' => esc_html__( 'Post', 'text-domain' ), 'post_type' => 'page', 'field_type' => 'select_advanced', 'class' => 'your-custom-class', ), ), ); return $meta_boxes; } );
Note that I added a
your-custom-class
to set a CSS class for your field. And then add the following code to your theme'sfunctions.php
file (or using a functionality/Code Snippet plugin):add_filter( 'rwmb_outside_conditions', function( $conditions ) { $conditions['.your-custom-class'] = [ 'visible' => ['post_type', 'page'], ]; return $conditions; } );
March 4, 2020 at 7:50 PM #18453Yumikom
ParticipantHi Anh,
Thank you for your teaching about Outside condition.
I solved what I wanted to do!Thanks!
-
AuthorPosts
- You must be logged in to reply to this topic.