Skip Navigation

[Resolved] Show custom taxonomy meta box only on specific pages with a template

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 0 replies, has 1 voice.

Last updated by jade 6 days, 12 hours ago.

Assisted by: Minesh.

Author
Posts
#2792318

Hi there,

I have created a couple of custom taxonomies with Toolset Types. I want to show these taxonomy meta boxes only on the editor pages that use a specific template, not all of the pages. Is there a way to do that? For your information, I use the latest version of WordPress and the Gutenberg block editor.

Thanks for your assistance!

#2792405

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Do you mean when user loggedin with Editor user role - you want to display taxonomy with specific conditoin? If yes - you want to display those taxonomy when editing post in backend or you want to display those taxonomy conditionally.

If you can share bit more information about your requirement and share few screenshot that should help me to understand your issue, once I review the information you shared I will be able to guide you in the right direction.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.

#2792649

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

There is no such native feature available.

I see you are using block based version and to remove the metaboxes for custom taxonomy I've added the following code to the "Custom Code" section offered by Toolset with code snippet namely "remove-tax-metaboxes":
=> hidden link

add_filter( 'rest_prepare_taxonomy', function( $response, $taxonomy ){
  
    $target_tax_slugs = array('job-position','location');
	if ( in_array($taxonomy->name,$target_tax_slugs) ) {
		$response->data['visibility']['show_ui'] = false;
	}
	return $response;
}, 10, 2 );

You can extend the above code to remove those taxonomy metaboxes panel conditionally based on your page IDs or search for a solutoin online.

#2793099

Thanks for the code. If possible, I would like to use a different approach outside of REST API because I want to use the page template as a conditional factor. Are there any other approaches outside of REST API?

Or if there is a way to identify the meta boxes, I can use that as well. I'm having trouble writing custom code because there is no ID on those custom taxonomy metaboxes. If they don't have ID natively, is there an option to give ID or something that can help identify the meta boxes?

#2793126

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Thanks for the code. If possible, I would like to use a different approach outside of REST API because I want to use the page template as a conditional factor. Are there any other approaches outside of REST API?
===>
Actually - as you are using block editor to edit your post that is why we need to use the REST API approach.

Fortunately - I found a workatound and I've adjusted the code added to "Custom Code" section with code snippet namely "remove-tax-metaboxes" as given under:
=> hidden link

add_action('admin_head', 'func_hide_or_remove_taxonomy_metabox_edit_post');
function func_hide_or_remove_taxonomy_metabox_edit_post() {
  global $post;
  
  $allowed_ids = array(731,653,655,714,719);

  if(!in_array($post->ID,$allowed_ids)) {
 echo '<script>
 wp.domReady( () => {
  const { removeEditorPanel } = wp.data.dispatch("core/edit-post");
  // Remove custom taxonomy panel from sidebar.
  removeEditorPanel( "taxonomy-panel-job-position" );
  removeEditorPanel( "taxonomy-panel-location" );
  } );
  </script>';
  }
}

Can you please confirm it works as expected.

#2793177

Thanks for the updated code! It works as expected.