I want to use the below code from an answer of @kaiser, but I don't understand how to use it in a real situation. An example here would be appreciated.
BTW, I don't see many Q&A about the wp_setup_nav_menu
filter.
add_filter( 'wp_setup_nav_menu', function( \stdClass $item ) {
# Check conditionals, and invalidate an item in case
if (
is_user_logged_in()
&& 'post' === $item->object
&& 'post_type' === $item->type
# && … whatever you need to check for your invalidation of an item
)
$item->_invalid = TRUE;
return $item;
} );