How to prevent duplicate posts with wp_insert_post on single.php? June 17, 2022 by Tarik Billa try putting your script in a function and running it during an action hook. perhaps the wp_footer hook.