Warfare has changed since World War II, specifically, the invention of “the bomb”. The US could handle a large military power that is kind of similar to itself, such as WWII Germany.
But these days any such challengers (such as Russia or China) are nuclear powers. The US typically goes to war with smaller nations, such as Vietnam or Iraq, which couldn’t possibly match the US in the field. As a result, they must use guerilla warfare. Guerillas beat Napoleon in Iberia, beat the US in Vietnam and at minimum confused the US in Iraq and Afghanistan.
Militarily defeating Vietnamese communists didn’t really work, not when the other alternative (supporting the undemocratic South Vietnamese dictatorship) wasn’t any better. Dropping bombs on Vietnam, unsurprisingly, did not win hearts and minds. And then the US ran into a problem: continuous warfare means continuous US military funerals. The loss of life was lesser but this wasn’t a massive struggle against evil like World War II, furthermore the war was never going to end.
Afghanistan was worse in some ways. The war lasted two decades. That’s practically a generation. And in a generation the country was still filled with people who think destroying girls’ schools is okay. How many generations was the US going to occupy Afghanistan to stop violent misogyny? Hearts and minds were not won, and probably cannot be won. The cultural gulf is simply too massive. You can’t use bullets to fix a cultural gulf.