Limit Number of Posts Per User in WordPress

Posted by on Aug 13, 2010 in How-To | 11 comments

This code will limit the number of posts a user can make.

At first, I was trying to limit each user to one post and I had that working. Then I realized that I needed to have different numbers of posts for different users, so I created an option in a user’s profile that only people with the right capability (manage_options) can edit. I’ll be changing this in the future for that number to be automatically changed depending on a user’s subscription type, but I needed this for testing purposes.

The option in the profile shows up under the contact info section, which I know is an odd place, but it was the easiest place to put it.

Put the following into your functions.php file:

// limit number of posts per user
// provided by cleverness.org
if ( !current_user_can('manage_options') ) {
	$default = 1; // default number of posts
	$count_posts = 0;
	global $wpdb;
	$poststable = $wpdb->prefix.'posts';
	$postlimit = get_user_meta($current_user->id, 'postlimit', true);
	if ( $postlimit == '' ) $postlimit = $default;
	$query = "SELECT COUNT(*) FROM $poststable WHERE post_author = '$current_user->id' AND (post_status = 'pending' OR post_status = 'draft' OR post_status = 'publish' ) ";
	$count_posts = $wpdb->get_var($query);
	if ($count_posts >= $postlimit) {
		if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )
			Header("Location: index.php");//redirects to dashboard
		if ( is_admin() ){
			$stylesheet =  get_stylesheet_directory_uri() . '/css/limitposts.css';
   			wp_register_style('limitpost_admin_css', $stylesheet, false, '1', 'screen');
   			wp_enqueue_style('limitpost_admin_css');
			}
	}
}

// add post limit option to profile
add_filter('user_contactmethods','hide_profile_fields',10,1);

function hide_profile_fields( $contactmethods ) {
	if( current_user_can( 'manage_options' ) )
		$contactmethods['postlimit'] = 'Post Limit';
  	return $contactmethods;
}

Create a file called limitposts.css, put the following into it, and save it in a directory called css in your theme folder (you can change where you save it, but be sure to change the location in functions.php).

#menu-posts .wp-first-item + li {
	display: none;
}

If you want to use this for custom post types, change:

if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )

to:

if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php?post_type=customposttype' )

and change the id name in your CSS:

#menu-posts-customposttype .wp-first-item + li {
	display: none;
}

Be sure to replace customposttype with the name of your custom post type.

11 Comments

  1. I want to limit post in woocommerce for products for vendor…
    But I can not work this codes. Perhaps problem is me.
    Please help me. Really I need that codes..

    Thank you so much. I contact with you twtiter and email.
    Have a nice day !

  2. thanks

  3. Hi, need help

    I added the code in my functions.php and showed error:

    Fatal error: Call to undefined function wp_get_current_user() in /home/storage/4/c5/a9/animaorkut/public_html/kraudiorails/wp-includes/capabilities.php on line 1281

    Thanks

    • You could try this instead in the functions.php file:

      add_action( 'plugins_loaded', 'cleverness_user_limits' );
      
      function cleverness_user_limits() {
      // limit number of posts per user
      // provided by cleverness.org
      if ( !current_user_can('manage_options') ) {
          $default = 1; // default number of posts
          $count_posts = 0;
          global $wpdb;
          $poststable = $wpdb->prefix.'posts';
          $postlimit = get_user_meta($current_user->id, 'postlimit', true);
          if ( $postlimit == '' ) $postlimit = $default;
          $query = "SELECT COUNT(*) FROM $poststable WHERE post_author = '$current_user->id' AND (post_status = 'pending' OR post_status = 'draft' OR post_status = 'publish' ) ";
          $count_posts = $wpdb->get_var($query);
          if ($count_posts >= $postlimit) {
              if ( $_SERVER['REQUEST_URI'] == '/wp-admin/post-new.php' )
                  Header("Location: index.php");//redirects to dashboard
              if ( is_admin() ){
                  $stylesheet =  get_stylesheet_directory_uri() . '/css/limitposts.css';
                  wp_register_style('limitpost_admin_css', $stylesheet, false, '1', 'screen');
                  wp_enqueue_style('limitpost_admin_css');
                  }
          }
      }
      }
      
      // add post limit option to profile
      add_filter('user_contactmethods','hide_profile_fields',10,1);
       
      function hide_profile_fields( $contactmethods ) {
          if( current_user_can( 'manage_options' ) )
              $contactmethods['postlimit'] = 'Post Limit';
          return $contactmethods;
      }
      

      Note: I have not tested this code. It may or may not work.

  4. Works great for me thanks.

  5. nice info,.,.
    thanks,.,.

  6. Is possible to change this code ‘/wp-admin/post-new.php’ by a different url, for exemple: ‘http://www.web.com/newform’ (“newform” There was a form that creates a post directly). Thanks

    • “newform” is a page, sorry.

  7. I used it but there are some other buttons present which are visible. and user can still add posts using those buttons. other scene is that user can directly change URL in browser and add posts. what is your suggestion for this situation?

    • Yes, this isn’t perfect. For any buttons that display, look to see what the CSS class or id is and use display: none for them. The URL is an issue that I didn’t look too far into since the userbase the site was for wouldn’t know to do that.

  8. I am also looking for a way to limit number of posts per user.Use your code,we can also public a post by quick post.We can public by other ways.so, i tried to change a user’s permissions to deal with,I think it’s more secure,but “edit_posts”=”new_posts”+”edit_posts”.Perhaps we need to find a more secure way!

Leave a Reply

Your email address will not be published. Required fields are marked *