• Hi – 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.

    any suggestions?

    thanks

    • This topic was modified 4 years, 10 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Everything else WordPress topic
Viewing 6 replies - 1 through 6 (of 6 total)
  • Are you wanting a plugin that accomplishes this or a piece of code that does this?

    Do you want to prevent creation as Publishing, meaning authors can create drafts but not publish posts or do you want to limit any post creation once the number is hit?

    Thread Starter wasanajones

    (@wasanajones)

    Hi – thanks for responding.

    plugin or code to add to functions.php, or even code to create my own plugin, would be great

    I want to give authors full freedom to create, publish, edit their posts. Backend okay (frontend editor not required but acceptable)

    What I need to do is limit the number of those posts.

    Ideally per month, per role.

    would be cool to allow authors to create draft and not be able to publish until the calendar rolls over. and/or even better if they upgrade their role (but that’s asking a lot of functionality).

    Role based would allow more flexibility, but need to keep in mind users could have more than one role.

    Very interested in what you might be able to stir up. Thanks

    If you’re willing to take a stab at this the boilerplate code is below:

    add_filter( 'wp_insert_post_data', function( $data, $postarr ) {
    	
    	$posts_per_month = 10; // Could be set as an updatable option
    	$user_id		 = get_current_user_id();
    	
    	// Number of posts in current month by this user
    	$author_posts = new WP_Query( array(
    		'post_type'		=> 'any',
    		'post_status'	=> 'publish',
    		'author'		=> $user_id,
    		'fields'		=> 'ids',
    		'monthnum'		=> date( 'm' ),	// Whatever the current month is
    	) );
    	
    	$author_post_count = $author_posts = $author_posts->post_count + 1; // Add current post
    	
    	if( $author_post_count > $posts_per_month ) {
    		$data['post_status'] = 'draft';
    	}
    	
    	return $data;
    	
    }, 10, 2 );

    Pretty much you want to query against the published posts date to todays date. If it surpasses your set limit then set it to draft. This could be extended further to display an admin notice and such. There are also some old plugins that do this which appear to still be compatible:

    Limit Posts by PluginCentral

    Post Creation Limits by Braintree

    Hopefully the above helps you and if you have any questions, comments, concerns I can try and address them!

    Thread Starter wasanajones

    (@wasanajones)

    Thank you, I’ll give it a run.

    So if I’m reading this, basically all authors would be limited to whatever we hard code in the snippet.

    any way to make it also based on role?

    $posts_per_month = 10; // Could be set as an updatable option
    $user_id = get_current_user_id();
    if role = x

    $posts_per_month = 20; // Could be set as an updatable option
    $user_id = get_current_user_id();
    if role =y

    I’ve tried the plugins you mentioned but they are completely unsupported by the devs and while there have been some kind folks to offer some fixes for outstanding issues I didn’t have a lot of confidence in them.

    Thanks again for your help.

    While not the preferred method, the current_user_can() function accepts roles for it’s check. I don’t see WordPress removing this functionality in the future:

    if( ! current_user_can( 'author' ) ) {
        return $data
    }

    If you want to be extra careful you can get the current user and check against their roles:

    $user = wp_get_current_user();
    
    if( empty( $user) || ! in_array( 'author', (array)$user->roles ) ) {
    	return $data;
    }
    Thread Starter wasanajones

    (@wasanajones)

    thank you very much

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Limit Post Creation Count by Author or Role’ is closed to new replies.