I'm trying to create a code snippet that will allow me to hide a menu item if a user has already posted at least 1 of a specific post type. I have this:
function hide_specific_menu_item($items, $args) {
$user_id = get_current_user_id(); //get the user_id
$post_type = "student"; // define the post type
$posts = count_user_posts( $user_id, $post_type ); //count user's posts
if($posts > 0){
foreach ($items as $key => $item) {
if ($item->title === 'Add Student') { //hide the menu item
unset($items[$key]);
}
}
}
return $items;
}
add_filter('wp_nav_menu_objects', 'hide_specific_menu_item', 10, 2);
But the above isn't working.
I would greatly appreciate it if someone could help me figure out where I'm going wrong.