Militants attacked a police post in northwestern Pakistan on Wednesday, triggering a gunbattle that killed five police officers and wounded about two dozen others, officials said.