1

How can I limit the number of posts created per category? And then delete the oldest post and save that one?

I would like to do something like this:

 add_action("load-post-new.php","limit_post_per_category");
  function limit_post_per_category(){
   $category = get_current_category(); //--not sure about this function....//
   if ($category == "category1") { 

    $category_post_count = count_posts($category);
    if($category_post_count>=10){
        delete the oldest post in the category;
        save post;
  }
 }
}
0

I would hook that on publish_post, so you aren't messing with drafts, etc.

And you need to account for multiple categories, and count the number of posts in each, and delete anything over 10.

Perhaps something like this will get you in the right direction. May need some tweaking, mostly untested.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.