The importance of Power Quality (PQ) issues in modern power systems has been growing in the last years. Capacitors are employed for optimizing network losses and increasing the voltage profile. However, in harmonic polluted networks, the placement of capacitors at their economic optimal locations may not be feasible due to harmonic constraints. Under such conditions, harmonic filters can mitigate harmonic pollution and enable capacitors to be optimally placed. This paper presents a novel approach for simultaneously optimizing the allocation of Active Power Filters (APFs) and capacitors, to improve the harmonic condition, network losses, and voltage profile of distribution networks. The method models APFs as harmonic sources in the Harmonic Power Flow (HPF) procedure, while capacitors are modelled as their corresponding harmonic admittance. Furthermore, a modified Particle Swarm Optimization (PSO) algorithm is presented as the optimization tool. Three case studies were conducted on the IEEE 18-bus test system. In the first study, optimal capacitor placement was performed with no regard to harmonic constraints, reducing network losses by 326 kW. The harmonic limits were then considered and satisfied by optimal APF placement. The total cost resulted in $241,983. The second study considered harmonic limits in the procedure of optimal capacitor placement, improving voltage profiles and network losses, but exceeding some harmonic constraints, which were subsequently satisfied by APF placement. The solution yielded the total cost of $264,942. The third study introduced simultaneous allocation of capacitors and APFs, proving to be the most cost-effective strategy ($225,417 total cost), promising enhanced network performance and efficiency, and adding valuable insights for future power system optimization.