Set a post expiration and delete a post when expirate
I'm trying to create a function that allows me to delete an article when the date of the day is equal or superior than a date filled in a custom ACF field.
I wrote the function below that I placed in my single.php in order to test it. It works, except that you must be reading the article in question for this function to execute (pretty logical actually).
What I want is that this function runs itself on all the articles, automatically, without having to go to the articles. This is where It's difficult for me, I often use plugins to do things like that.
Could you show me some ways to achieve the desired result? I'm really looking for improve my back-end skills using Wordpress, so I'm not asking particulary for a ready-made solution but ways to guide my work.
Here's the function :
/**
* Draft after expiration
*/
function draft_the_post(){
$expire_date = get_field( "field_5cef86384e5f2" );
$actual_date = date("d-m-Y");
$postid = $post-ID;
if ($expire_date = $actual_date) {
wp_update_post(array(
'ID' = $postid,
'post_status' = 'draft'
));
} else {
echo "Not same date or inferior than today";
};
}
Oh and, excuse my english I'm French :-)
I thank you in advance, willwoody.