最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Limit Post Creation Count by Author or Role

programmeradmin3浏览0评论

I need to limit the number of posts that can be created.

I'd like any variation of total posts, posts per time period (month), by author (ID) or by role.

off-the-shelf plugins found so far in the repository are no longer supported.

is there a functions.php snippet or other approach that works consistently?

any suggestions?

thanks

I need to limit the number of posts that can be created.

I'd like any variation of total posts, posts per time period (month), by author (ID) or by role.

off-the-shelf plugins found so far in the repository are no longer supported.

is there a functions.php snippet or other approach that works consistently?

any suggestions?

thanks

Share Improve this question asked Apr 27, 2020 at 6:03 Trip Vendors IncTrip Vendors Inc 477 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

I have a site that offers non-paying members up to 5 posts using this:

function author_post_count($user_id, $post_type = array('YOUR_CUSTOM_POST_TYPE_NAME_HERE')) {
    $args = array(
        'post_type'         => $post_type,
        'author'            => $user_id,
        'posts_per_page'    => -1
    );

    $query = new WP_Query($args);
    return $query->found_posts;
}

Then I use:

if ( author_post_count(get_current_user_id()) >5) {
SHOW RESTRICTION NOTICE CODE HERE FOR USERS WITH MORE THAN 5 POSTS 
}

So you could use those two together and then the save_post hook. If you're not familiar, read up on it and in particular this part about avoiding a loop: https://developer.wordpress/reference/hooks/save_post/#avoiding-infinite-loops

Then you could change the query to use Date Parameters

There is a function in WordPress name count_user_posts which accept two parameters, first is $userid and second is $post_type. So, providing this function the user ID and post type you can check if the user has more than 5 posts or not.

In code this would look a more like below-

// Assuming user ID is 5 and the post type is 'post'
if ( count_user_posts( 23 , 'post' ) > 5 ) {
    // Do what you prefer if the post count is more than 5.
}

发布评论

评论列表(0)

  1. 暂无评论