This paper introduces a novel swarm-based metaheuristic called swarm magnetic optimizer (SMO). SMO imitates the behaviour of two magnets close to each other: pushing toward or pulling away from each other. This pushpull mechanism is then adopted to become a novel search in SMO. SMO is set up as a swarm of magnets that move autonomously. In the early iteration, the pull strategy is dominant. Meanwhile, it declines during the iteration and is replaced by a push strategy. The determination between these two strategies is calculated stochastically. SMO consists of three sequential phases in every iteration where the corresponding magnet and its reference run a push or pull search in each phase. The reference used in each phase is the global best magnet, a randomly selected magnet, and a randomly generated magnet. This paper evaluates SMO through simulation to find the optimal solution for 23 functions. The performance of SMO is compared with five latest metaheuristics: mixed leader-based optimizer (MLBO), guided pelican algorithm (GPA), zebra optimization algorithm (ZOA), coati optimization algorithm (COA), and clouded leopard optimizer (CLO). The result shows that SMO is better than MLBO, GPA, ZOA, COA, and CLO in consecutively 22, 15, 11, 13, and 14 functions. The superiority of SMO, especially, is in solving high dimension functions. Meanwhile, in the fixed-dimension multimodal functions, SMO is still superior to MLBO but less superior to GPA, ZOA, COA, and CLO.