Is it possible to delete/move to trash a post in a single category each time that a new post in the same category is created? I mean, I need to always keep just one post (the most recent) visible in that one category.
Is it possible to delete/move to trash a post in a single category each time that a new post in the same category is created? I mean, I need to always keep just one post (the most recent) visible in that one category.
Share Improve this question asked Sep 26, 2019 at 21:15 andreiabcandreiabc 11 Answer
Reset to default 0Why not. The API of WordPress has the function wp_delete_post
to delete a post. If the trash is active, on default true, then this function moves the post to the trash.
You need to add a logic after publishing a new post to delete the old one. To firte your function you should use the hook publish_post
.
So a simple starter see below, need the logic to get the ID of the old post.
add_action( 'publish_post', 'post349197', 10, 2 );
function post349197( $ID, $post ) {
// &oldID = Need logic to get the last post.
wp_delete_post($oldId);
}