We present a novel method to compute unstable periodic orbits (UPOs) that optimize the infinite-time average of a given quantity for polynomial ODE systems. The UPO search procedure relies on polynomial optimization to construct nonnegative polynomials whose sublevel sets approximately localize parts of the optimal UPO, and that can be used to implement a simple yet effective control strategy to reduce the UPO's instability. Precisely, we construct a family of controlled ODE systems parameterized by a scalar k such that the original ODE system is recovered for k = 0, and such that the optimal orbit is less unstable, or even stabilized, for k > 0. Periodic orbits for the controlled system can be more easily converged with traditional methods and numerical continuation in k allows one to recover optimal UPOs for the original system. The effectiveness of this approach is illustrated on three low-dimensional ODE systems with chaotic dynamics.