He thought, based on Nancy Pelosi’s support, democrats would protect his position. I think he chose poorly.
The Democrats probably told him that if he made this deal to prevent the shutdown, they'll protect him from a challenge by Matt Gaetz. Guess what? They didn't do that. But that's on McCarthy. He got snakebit in the swamp.