I am having some problems and also some questions regarding how can one get the general solution to the Klein-Gordon equation, which usually appears in the literature as $$ \phi(t,\mathbf{x})=\int\dfrac{d^3p}{(2\pi)^3}\dfrac{1}{2E_\mathbf{p}} \left(a(\mathbf{p})e^{-i(E_\mathbf{p}t-\mathbf{p}\cdot\mathbf{x})} + a^*(\mathbf{p})e^{i(E_\mathbf{p}t-\mathbf{p}\cdot\mathbf{x})}\right) $$ and the answers to other similar questions do not solve my issues at all.
First, for the ansatz $$ \phi(t,\mathbf{x}) = e^{-i p_\mu x^\mu} $$ to be a solution of the K-G equation, the dispersion relation $$ (p^0)^2 - E_{\mathbf{p}}^2 = 0\quad\text{for}\quad E_\mathbf{p} = \sqrt{\mathbf{p}^2+m^2} $$ must hold. Therefore, we can write the general solution as the mode expansion $$ \phi(t,\mathbf{x}) = \int\dfrac{d^4p}{(2\pi)^4} a(p^0,\mathbf{p}) e^{-i p_\mu x^\mu} \delta((p^0)^2 - E_\mathbf{p}^2) $$
Now, we have that $$ \delta((p^0)^2 - E_\mathbf{p}^2) = \dfrac{1}{2E_\mathbf{p}} \left(\delta(p^0 - E_\mathbf{p}) + \delta(p^0 + E_\mathbf{p})\right) $$ Plugin this into the previous integral, and integrating over $p^0$, we get $$ \phi(t,\mathbf{x}) = \int\dfrac{d^3p}{(2\pi)^4} \dfrac{1}{2E_\mathbf{p}} \left( a(E_\mathbf{p},\mathbf{p}) e^{-i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} + a(-E_\mathbf{p},\mathbf{p}) e^{-i (-E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \right) $$
Now, I see two problems with this solution. First one, I am missing some $2\pi$ factor that should cancel one of the first denominator so I get the factor $$ \dfrac{d^3p}{(2\pi)^3} $$ right. And second, the signs in the exponents seem to be wrong also, not only because they don’t match the original solution, but also because if I try to impose the real condition $$ \phi(t,\mathbf{x}) = \phi^*(t,\mathbf{x}) $$ to get the relationship between the coefficients, I cannot match the exponentials.
What am I missing? Maybe I did the integration over $p^0$ and the delta functions wrong?
Also, I see that some solutions add the condition $\theta(p^0)$ to the mode expansion. I understand the idea of considering only positive-energy particles, but would not that remove the second exponential from the original solution? How is that Heaviside function compatible with the mode expansion that appears in almost any QFT book?
Update
I came up with an idea for matching exponentials when imposing that the field must be real, though I would like some feedback on its validity or to know other approaches.
As it is right now, we have these interesting terms: $$ \text{Terms of } \phi:\quad a(E_\mathbf{p},\mathbf{p}) e^{-i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \quad\text{and}\quad a(-E_\mathbf{p},\mathbf{p}) e^{-i (-E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \\ \text{Terms of } \phi^*:\quad a^*(E_\mathbf{p},\mathbf{p}) e^{i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \quad\text{and}\quad a^*(-E_\mathbf{p},\mathbf{p}) e^{i (-E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} $$ The signs of the exponentials do not match, so I cannot get something like $$ a(-E_\mathbf{p},\mathbf{p}) = a^*(E_\mathbf{p},\mathbf{p}) $$ as I was expecting.
However, since $E_\mathbf{p}$ depends on $\mathbf{p}^2$, I can make a change of variable to invert the sign of $\mathbf{p}$ in the second term of the integral. Thus, I have $$ \phi(t,\mathbf{x}) = \int\dfrac{d^3p}{(2\pi)^4} \dfrac{1}{2E_\mathbf{p}} \left( a(E_\mathbf{p},\mathbf{p}) e^{-i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} + a(-E_\mathbf{p},-\mathbf{p}) e^{-i (-E_\mathbf{p}t + \mathbf{p}\cdot\mathbf{x})} \right) $$
Now, comparing this with its complex conjugate version, $$ \text{Terms of } \phi:\quad a(E_\mathbf{p},\mathbf{p}) e^{-i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \quad\text{and}\quad a(-E_\mathbf{p},-\mathbf{p}) e^{-i (-E_\mathbf{p}t + \mathbf{p}\cdot\mathbf{x})} \\ \text{Terms of } \phi^*:\quad a^*(E_\mathbf{p},\mathbf{p}) e^{i (E_\mathbf{p}t - \mathbf{p}\cdot\mathbf{x})} \quad\text{and}\quad a^*(-E_\mathbf{p},-\mathbf{p}) e^{i (-E_\mathbf{p}t + \mathbf{p}\cdot\mathbf{x})} $$ and I can match the exponentials and get $$ a(-E_\mathbf{p},-\mathbf{p}) = a^*(E_\mathbf{p},\mathbf{p}) $$
However, I am still missing that $2\pi$ factor.