Limit number of custom post type posts

2020-05-10 06:12发布

问题:

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.

回答1:

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.



标签: wordpress