Trump doesn't care one way or the other about GLBT people. But, he made allies of the Christian religious right to get elected, and he'll throw GLBT people under the bus at any time to keep the Christian religious right in his camp.
I think his July tweets came when one or more of his sons got into trouble over Russia. So he needed something to keep his base happy. And, boy, is the base happy with this one. After all, it's not about bias. It's about national security, and not messing with God's natural order. Trump scored a two-fer.
The tweets came with no consultation with the armed forces upper brass, who were caught completely by surprise.
Gen. Mattis seems like a heads-up guy. I wonder if he's keeping the status quo for the next six months or so as a stalling tactic while Trump gets distracted by something else.
Mattis knows that Trump is often gung-ho on something -- until the next advisor walks into his office.