Limit number of custom post type posts

2020-05-10 05:45发布

Is it possible to limit the number of posts a admin can create in a custom post type? It's for a plugin and the admin should only be able to create 10 posts. Thanks.

标签: wordpress
1条回答
虎瘦雄心在
2楼-- · 2020-05-10 06:09

Here you go (untested, but you should get the idea):

function check_allowed( $post_id ) {

    $current_user = wp_get_current_user();


    $q = new WP_Query(array('post_type'=>'limited','post_author'=>$current_user->ID));

    if($q->found_posts >=10) {
         return false;
     }
}

add_action( 'publish_post', 'check_allowed' );

Btw: You probably do not want to limit admins in the number of posts they may publish, use editors or a custom role for this purpose.

查看更多
登录 后发表回答