Support Forum
Support › Meta Box Conditional Logic › Hide a metabox when a parent is selected
Hi,
I would like to hide some metaboxes (from Meta Box & WordPress) when user select a Parent in the WP dropdown.
Is it possible with Conditional Logic ?
Thank You
mmmm looks like https://support.metabox.io/topic/if-is-parent/ lol
May be i've chosen the wrong extension :p ?
Ok... it's workin perfectly for meta-boxes with
'visible' => array('parent_id', '=', ''),
For notice, a comma is missing in the doc
'visible' => array('parent_id', '!=' '')
But how to with the WordPress Metaboxes ? (For example postexcerpt ou a taxonomy_div for a CPT)
Thank You !!
Hi Sinusite,
Thanks for purchasing Conditional Logic and reporting documentation missing, you rock man 🙂
For postexcerpt or taxonomy_div, you can add filter to rwmb_outside_conditions like so:
add_filter( 'rwmb_outside_conditions', function( $conditions )
{
$conditions['postexcerpt'] = array(
'hidden' => array(
'relation' => 'and',
'when' => array( array('post_format', '=', 'aside') )
)
);
return $conditions;
} );
P/S: The conditional logic array should be simplified like example below but it's a bug on our extension and you'll get the update on the next 24 hour:
$conditions['postexcerpt'] = array(
'hidden' => array('post_format', 'aside' )
);
Thanks and Best regards
Tan Nguyen
Also, we've released 1.0.4 and you can use simplified syntax like example above, have a nice day and don't hesitate to tell us if you have any problem 🙂
Hi Tan,
Thanks for you quick answer and amazing work.
What is the best syntax for multiple fields to hide with the same conditions ?
i.e. :
$conditions['tagsdiv-maitreo'] = array( 'hidden' => array('parent_id', '>', 0 ) );
$conditions['postimagediv'] = array( 'hidden' => array('parent_id', '>', 0 ) );
$conditions['sbg_box'] = array( 'hidden' => array('parent_id', '>', 0 ) );
regards.
Hi Sinusite,
Great question, I will implement update to get multiple field hide with same condition, currently, this version doesn't support but you can try this snippet for shorter and easier to update. Basically, assign hidden conditions to a variable called $hidden_condition
and attach that condition to all of your meta boxes:
$hidden_condition = ['hidden' => ['parent_id', '>', 0]];
foreach ( ['tagsdiv-maitreo', 'postimagediv', 'sbg_box'] as $metabox ) {
$conditions[$metabox] = $hidden_condition;
}
Best Regards
ok Thanks !
Last question (before the next one :p ):
What does your "Meta Box Include Exclude" plugin can bring me more ?
Hi Sinusite,
Thanks again for your question you're welcome to post any question about our Meta Box so please don't hesitate to tell me if you have problems 🙂
As noted in documentation:
"This extension shows/hides meta boxes when page loads or after saving. It does not include Javascript code to toggle meta boxes immediately when conditions changed."
The different between Conditional Logic and Include / Exclude basically is:
- Conditional Logic run when field values change immediately while Include/Exclude run conditions on page load.
- Conditional Logic can hide meta box, fields or any DOM element while Include/Exclude only works with meta box.
- Conditional Logic only hide elements, the element still exists while Include/Exclude can totally remove element from the page.
Best regards
Tan Nguyen