Does anyone know how to delete post from WordPress if not exist in API using PHP? I have script that import cars from API to posts in WordPress but I don’t know how to delete the post in WordPress if the post is deleted from API.
Advertisement
Answer
I am assuming that when you say API you’re talking about an external API that you use to fetch the car’s data.
So if the car has been removed from external API then you want it to be deleted from WordPress as well.
To do that, you must have unique key data in the post, it could be SKU or anything else, this key will help you match/find the post. then you need to write a delete script where you fetch data API from API then collect all the unique keys or SKUs, then find all the posts excluding those unique keys. In this way, you’ll get all the posts with unique keys that no more exists in your API and then you can delete those posts.
You can run this script with a button click or run it with wordpress cron.