Photocatalytic seawater splitting is a promising method for producing hydrogen peroxide (H2O2). However, the presence of metal ions usually deactivates catalysts and causes undesirable reactions, highlighting the need for deeply understanding these interactions. Herein, a carbon-based composite photocatalyst (CQM) is fabricated for photocatalytic production of H2O2. The metal ions strongly affect the photocatalytic activity of the CQM catalyst with order of Mg2+ > Al3+ > Ca2+ > K+, consistent with the surface effective electron concentration calculated from transient photovoltage (TPV) technology. Notably, with the presence of Mg2+ at 0.36 mol L−1, the H2O2 yield is 12,812 μmol g−1 h−1. Under the guidance of machine learning, the H2O2 yield is further improved to 19,560 μmol g−1 h−1 by adjusting the concentration of multiple ions. Significantly, in a real seawater system, the photoproduction of H2O2 on CQM reaches up to a recorded value of 11306 μmol g−1 h−1 under visible light illumination.