<?php
/**
* Plugin Name: Hide Fields if not Industries
*/
add_action(
'admin_head',
function(){
// Are we not editing a post / page?
if (
! array_key_exists( 'post', $_GET )
|| ! array_key_exists( 'action', $_GET )
) {
// Don't do anything.
return;
}
// Is the parent slug not 'industries'?
$parent = get_post(
get_post( $_GET['post'] )->parent
);
if (
'industries' !== $parent->post_name
) {
// Don't do anything.
return;
}
?>
<style>
/* Hide a specific field named "field-name" */
.pods-form-ui-row-name-field-name,
/* Hide a field group named "more-fields" */
#pods-meta-more-fields
{
display: none;
}
</style>
<?php
}
);
Thank you, that got me 90% of the way. I did have to change to post_parent as it’s a page.
Here’s my final working code with the changed meta box div ID too.
/**
* Plugin Name: Hide Fields if not Industries
*/
add_action(
'admin_head',
function () {
// Only run on post.php edit screen.
if (
! isset( $_GET['post'], $_GET['action'] )
|| 'edit' !== $_GET['action']
) {
return;
}
$post_id = (int) $_GET['post'];
$post = get_post( $post_id );
if ( ! $post || 'page' !== $post->post_type ) {
return;
}
// Get parent post.
if ( ! $post->post_parent ) {
// No parent, so parent is not "industries" -> hide meta box.
$parent_slug = null;
} else {
$parent = get_post( $post->post_parent );
$parent_slug = $parent ? $parent->post_name : null;
}
// Hide meta box if parent slug is NOT "industries".
if ( 'industries' === $parent_slug ) {
// Parent is "industries", so do nothing, leave visible.
return;
}
?>
<style>
/* Hide the Pods meta box when parent is not "industries" Change #pods-meta-case-studies to whatever your div ID is for your meta box. You can find this by inspecting the edit page screen */
#pods-meta-case-studies {
display: none;
}
</style>
<?php
}
);