我会把它挂上publish_post
, 这样你就不会弄乱草稿等了。
你需要考虑多个类别,计算每个类别中的帖子数量,删除超过10个的帖子。
也许这样的事情会让你走上正确的方向。可能需要一些调整,大部分未经测试。
function on_post_publish( $ID, $post ) {
$cat = get_the_category( $ID ); //returns array of categories
foreach ($cat as $c) { //doing the rest for each cat in array
$args = array(
\'orderby\' => \'post_date\', //using the post date to order them
\'order\' => \'ASC\', // putting oldest at first key
\'cat\' => $c, // only of the current cat
\'posts_per_page\' => -1, //give us all of them
\'fields\' => \'ids\' //only give us ids, we dont want whole objects
);
$query = new WP_Query( $args );
$count = $query->post_count; //get the WP_Queries post_count object value
if ($count > 10) { // if that value is more than 10
$id_to_delete = $query->posts[0]; //get oldest post from query
wp_delete_post( $id_to_delete ); //delete it
}
}
wp_reset_postdata(); //resetting to main query just in case
}
add_action( \'publish_post\', \'on_post_publish\', 10, 2 );