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

2 Answers 2

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.org/reference/hooks/save_post/#avoiding-infinite-loops

Then you could change the query to use Date Parameters

0

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.
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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