最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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);
}
发布评论

评论列表(0)

  1. 暂无评论