When registering metabox with conditional logic ->where( 'post_template', '=', 'template-contact.php' ) it works as expected with post type page but is also visible on every custom post type out there. Not so nice, but solvable by using also ->where( 'post_type', '=', 'page' ).
We come to problems with (quite rare) use case, where metabox needs to be visible on specific template and post ID which belongs to post in some CPT. Like this
->where( 'post_template', '=', 'template-contact.php' )
->where( 'post_id', '=', '25' ) // post in service CPT
With those conditional logic declarations, metabox comes visible everywhere and chaining up conditional logic declarations like below won't work either
->where( 'post_template', '=', 'template-contact.php' )
->where( 'post_type', '=', 'page' )
->or_where( 'post_id', '=', '25' ) // post in service CPT
Is this something that can be solved or something that we need to live with?
If I've understood correctly, this is what you need:
->where( function( $condition ) {
$condition->where( 'post_type', '=', 'page' );
$condition->where( 'post_template', '=', 'template-contact.php' );
} )
->or_where( 'post_id', '=', '25' ) // post in service CPT
You understood me correctly and it should go like that :) Actually I tried similar custom comparison also, but it didn't nor does your example work. For me it seems like post_type condition is still kicking in when checking the ID.
Thank you for the report @timiwahalahti - I was able to reproduce it locally so we'll be taking a look at this for the next release.
I've pushed a fix in the development branch which will make it into the next release.
In the mean time you can use this workaround:
->where( function( $condition ) {
$condition->where( 'post_type', '=', 'page' );
$condition->where( 'post_template', '=', 'YOUR_TEMPLATE_NAME_HERE.php' );
} )
->or_where( function( $condition ) {
$condition->where( 'post_type', '=', 'YOUR_CUSTOM_POST_TYPE_HERE' );
$condition->where( 'post_id', '=', 12345 );
} )
2.1.0 has been released which addresses this issue:
https://github.com/htmlburger/carbon-fields/releases/tag/2.1.0