Hi, I’m trying to prevent non-admin users from posting to a specific category in HivePress. Currently, I am using the following code to hide category ID 66 from the category selection for non-admin users.
Here is the code I’m using:
// Hide category ID 66 from non-admin users on the listing submission form
add_filter( 'hivepress/v1/forms/listing_submit', function( $form ) {
if ( ! current_user_can( 'manage_options' ) && isset( $form['fields']['categories'] ) ) {
$restricted_category_id = 66;
// Remove category ID 66 from the options
$new_options = [];
foreach ( $form['fields']['categories']['options'] as $term_id => $label ) {
if ( $term_id != $restricted_category_id ) {
$new_options[ $term_id ] = $label;
}
}
$form['fields']['categories']['options'] = $new_options;
}
return $form;
}, 100 );
Is there a better way to achieve this? If the current code needs improvement, could you please point it out?
Thanks in advance!