automation - Automatically delete post in a single category when a new one is created

Is it possible to deletemove to trash a post in a single category each time that a new post in the same category is cre

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 1
Add a comment  | 

1 Answer 1

Reset to default 0

Why 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);
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745125370a4612659.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信