“Recently I was reading Ps. 38 and was shocked to read that David’s ‘loins were filled with a loathsome disease and there was no soundness in his flesh.’ Did that mean that he had some type of STD?”

Interesting question, indeed;

But, we must be careful in engaging in “spiritual speculation:” especially on such a subject as this.
While our minds are naturally inquisitive. . .and David’s words in v.7 do sound like someone possibly suffering from an STD. . .especially in light of his one-night, adulterous affair with Bathsheba. . .it’s wrong to jump to a conclusion such as that: for, in reality, only God knows.

Yes, the word “loathsome” in the KJV can be translated “burning, roasted, parched, etc.” And, interestingly, the word “disease” is not present in the original manuscript; it was added later by editors or redactors of the text to aid in understanding.
Likewise, the word “soundness” basically means “wholeness;” so it could infer that there was some type of “wasting away.” But, the word “loins” doesn’t necessarily refer to one’s “private parts;” instead, it can the “fatness” that may even form on one’s internal organs.

Thus, it’s possible that it was some type of internal malignancy, ulcer, osteoporosis, hyperparathyroidism, etc. So, again, we’d do well to stick with the “big picture” of David’s struggling with guilt over his sins instead of speculating on what the “wages” of his sins might have been.

And, even if it was an STD, does that mean David never was a “man after God’s Own Heart,” i.e., “in pursuit of having a heart like God’s?”

No, not in the least;

Instead, it would show that David was human just like us. . .and reaped the consequences of his sins (just like us). . .but thankfully knew how to find forgiveness, cleansing and restoration through confession and repentance (Ps. 51; 32).

That’s why we call it “Amazing Grace:”

And that’s why His “Mercies keep us from getting what we deserve” (Lam. 3:22).

Amen and amen.

Thank You, Jesus.