### Godot version

v4.0.3.stable.mono.official [5222a99f5]

### System information

Windows 11

### Issue description

In the "Select a Node" windows, popped up when pressed the "Assign..." button of a `NodePath` property in the inspector.
Input any character in the "Filter Nodes" line, then clear it.

All ancestors of target type nodes become selectable.

I think it's because the correct result is supposed to be like this:
![image](https://github.com/godotengine/godot/assets/3178725/7d8ef6a2-d9f0-444e-8b18-0f251d553a1d)

but the invalid ancestors, which should be leaved in the dialog but set to unselectable, are in the wrong state.



### Steps to reproduce

- Create a new project.
- Create a new scene like this.
![image](https://github.com/godotengine/godot/assets/3178725/84acde89-a6a9-4ba7-adb4-f883531bd51a)
- Select `BoneAttachment3D` node, turn on `Use External Skeleton`
![image](https://github.com/godotengine/godot/assets/3178725/fd9fd252-253d-4a48-ba2b-764cef276738)
- Hit the "Assign..." button, open the dialog, it's good at this moment.
![image](https://github.com/godotengine/godot/assets/3178725/511d6fb8-4c83-408b-9717-0726cb7568ad)
- Input any character in the "Filter Nodes" line (like `d`).
![image](https://github.com/godotengine/godot/assets/3178725/cc60b4e4-c890-4c81-ae16-edc28fc18b57)



### Minimal reproduction project

N/A
for (int i = 0; i < p_terms.size(); i++) {
		String term = p_terms[i];
		// Recognize special filter.
		if (term.contains(":") && !term.get_slicec(':', 0).is_empty()) {
			String parameter = term.get_slicec(':', 0);
			String argument = term.get_slicec(':', 1);
			if (parameter == "type" || parameter == "t") {
				// F
				String type = get_node(p_item->get_metadata(0))->get_class();
				bool term_in_inherited_class = false;
				// E
				while (type != "Node") {
					if (type.to_lower().contains(argument)) {
						term_in_inherited_class = true;
						break;
					}
					type = ClassDB::get_parent_class(type);
				}
				if (!term_in_inherited_class) {
					return false;
				}
			} else if (parameter == "group" || parameter == "g") {
				// F
				Node *node = get_node(p_item->get_metadata(0));
				if (argument.is_empty()) {
					// W
					if (node->get_persistent_group_count() == 0) {
						return false;
					}
				} else {