WordPress limit post that subscriber can create

This solution uses the publish post hook. What’s happening here is that when a post is published, it runs the custom post_published_limit function we create below.

The $max_posts is set statically here, you can expand it to pull the value from elsewhere, but that’s beyond the scope of the question being asked. However there is plenty of literature describing how to do that.

The function then gets the count of posts of the author currently trying to publish.

If the $count exceeds the $max_posts the function will save it as a draft instead of publishing it.

function post_published_limit( $ID, $post ) {
    $max_posts = 5; // change this or set it as an option that you can retrieve.
    $author = $post->post_author; // Post author ID.
    $count = count_user_posts( $author, 'post'); // get author post count

    if ( $count > $max_posts ) {
        // count too high, let's set it to draft.
        $post->post_status="draft";
        wp_update_post( $post);
    }
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );