User Posts Limit

How to set post creation limit for authors?
In the plugin settings set ‘Author’ in role, ‘Posts’ in post type and the limit.

How to make rules that applied on certain post type to limit per website instead of per user role?
Update the post type:

add_filter( 'upl_query', 'upl_limit_total_posts' );
add_filter( 'upl_rule_limit_current_user_role_check', 'upl_convert_user_role', 10, 2 );

function upl_limit_total_posts( $args ) {
	$post_type = 'post';
	if ( $post_type === $args['post_type'] ) {
		unset( $args['author'] );
	}
	return $args;
}

function upl_convert_user_role( $check, $i ) {
	$post_type = 'post';
	return $post_type === get_option( 'upl_posts_type' )[ $i ] && ! is_user_logged_in() ? true : $check;
}

How to make rules that applied on certain post type to limit by posts in specific categories only?
Update the post type and the categories id (make sure users can’t migrate posts from/into unrestricted posts categories):

add_filter( 'upl_query', 'upl_specific_category' );
function upl_specific_category( $args ) {
	$post_type = 'post';
	$category_id = [ '4', '2' ];
	if ( $post_type === $args['post_type'] ) {
		$args['cat'] = $category_id;
	}
	return $args;
}

How to make rules that applied on certain post type to limit only specific post statuses?
Update the post type and the post statuses (make sure users can’t migrate posts from/into unrestricted post status):

add_filter( 'upl_query', 'upl_specific_post_status' );
function upl_specific_post_status( $args ) {
	$post_type = 'post';
	$post_status = [ 'any' ];
	if ( $post_type === $args['post_type'] ) {
		$args['post_status'] = $post_status;
	}
	return $args;
}

How to modify the cycle for rules that applied on certain post type?
Update the post type and the cycle:

add_filter( 'upl_query', 'upl_modify_cycle' );
function upl_modify_cycle( $args ) {
	$post_type = 'post';
	$cycle = '3 days ago';
	if ( $post_type === $args['post_type'] ) {
		$args['date_query']['after'] = $cycle;
	}
	return $args;
}

How to display to the users only the limits that applied on them based on the selected ‘Priority’ option?

add_filter( 'upl_display_prioritized_rules_enabled', '__return_true' );

How to prevent users to exceed the limits by opening multiple ‘new post’ windows?

add_filter( 'upl_skip_auto_draft_enabled', '__return_true' );

How to hide the ‘Add New’ button when posts limit exceeded?

add_filter( 'upl_hide_add_new_button_enabled', '__return_true' );

How to define limits for the total number of multiple post types?
Update the packages’ names and the post types they include:

add_action( 'init', 'upl_create_packages' );
add_action( 'admin_init', 'upl_add_author_support' );
add_filter( 'upl_rule_limit_current_post_type_check', 'upl_convert_post_types', 10, 3 );
add_filter( 'upl_query', 'upl_apply_packages', 10, 2 );

function upl_get_packages_types() {
	return [
		'Basic Package' => [ 'post', 'page' ],
		'Premium Package' => [ 'post', 'page', 'media' ]
	];
}

function upl_create_packages() {
	foreach ( array_keys( upl_get_packages_types() ) as $package ) {
		register_post_type( sanitize_key( $package ), [ 'labels' => [ 'name' => $package ] ] );
	}
}

function upl_add_author_support() {
	if ( current_user_can( get_option( 'upl_manage_cap' ) ) ) {
		$post_types = [];
		foreach ( upl_get_packages_types() as $package ) {
			$post_types = array_merge( $post_types, $package );
		}
		foreach ( $post_types as $post_type ) {
			add_post_type_support( $post_type, 'author' );
		}
	}
}

function upl_convert_post_types( $check, $i, $current_post_type ) {
	foreach ( upl_get_packages_types() as $package => $types ) {
		if ( sanitize_key( $package ) === get_option( 'upl_posts_type' )[ $i ] && in_array( $current_post_type, $types ) ) {
			return true;
		}
	}
	return $check;
}

function upl_apply_packages( $args, $i ) {
	foreach ( upl_get_packages_types() as $package => $types ) {
		if ( sanitize_key( $package ) === get_option( 'upl_posts_type' )[ $i ] ) {
			$args['post_type'] = $types;
			break;
		}
	}
	return $args;
}

How to lock the edit of posts for some user role when certain condition are met?
Update the post type, the role, and the condition:

add_filter( 'get_post_metadata', 'upl_lock_posts', 10, 5 );
function upl_lock_posts( $value, $object_id, $meta_key, $single, $meta_type ) {
	$post_type = 'post';
	$role = 'editor';
	$condition = 'active' !== get_user_meta( get_current_user_id(), 'membership_level', true );
	$admin_id = '1';
	return '_edit_lock' === $meta_key && $post_type === get_post_type( $object_id ) && in_array( $role, wp_get_current_user()->roles ) && $condition ? time() . ':' . $admin_id : $value;
}

How to make rules that apply on certain post type to limit by the posts of the current user instead of the assigned owner (suitable for frontend forms that create the post as admin and are modified only afterwards)?
Update the post type:

add_filter( 'upl_query', 'upl_limit_current_user' );
function upl_limit_current_user( $args ) {
	$post_type = 'post';
	if ( $post_type === $args['post_type'] && ! is_admin() ) {
		$args['author'] = get_current_user_id();
	}
	return $args;
}

How to allow posts limit per user (will be displayed on the Edit User Screen)?

add_filter( 'upl_rule_num_limit', 'upl_user_num_limit', 10, 3 );
add_action( 'show_user_profile', 'upl_add_num_limit_user_data' );
add_action( 'edit_user_profile', 'upl_add_num_limit_user_data' );
add_action( 'personal_options_update', 'upl_save_num_limit_user_data' );
add_action( 'edit_user_profile_update', 'upl_save_num_limit_user_data' );
add_filter( 'manage_users_columns', 'upl_num_limit_user_table' );
add_filter( 'manage_users_custom_column', 'upl_num_limit_user_table_row', 10, 3 );

function upl_user_num_limit( $num_limit, $i, $user ) {
	$user_limit = get_user_meta( $user, 'num_limit', true );
	return '' !== $user_limit ? $user_limit : $num_limit;
}

function upl_add_num_limit_user_data( $user ) {
	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		?>
		<h3><?php esc_html_e( 'User Posts Limit', 'user-posts-limit' ); ?></h3>
		<table class="form-table">
			<tr>
				<th><label for="num_limit"><?php esc_html_e( 'Limit', 'user-posts-limit' ); ?></label></th>
				<td>
					<input type="number" min="0" name="num_limit" value="<?php echo esc_attr( get_user_meta( $user->ID, 'num_limit', true ) ); ?>" class="regular-text" />
					<p class="description"><?php esc_html_e( 'It will override the limit of all the rules that applies on this user', 'user-posts-limit' ); ?></p>
				</td>
			</tr>
		</table>
		<br>
		<?php
	}
}

function upl_save_num_limit_user_data( $user_id ) {
	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		update_user_meta( $user_id, 'num_limit', sanitize_text_field( $_POST['num_limit'] ) );
	}
}

function upl_num_limit_user_table( $columns ) {
	$columns['num_limit'] = __( 'Limit', 'user-posts-limit' );
	return $columns;
}

function upl_num_limit_user_table_row( $row_output, $column_id_attr, $user_id ) {
	return 'num_limit' === $column_id_attr ? get_user_meta( $user_id, 'num_limit', true ) : $row_output;
}

71 responses to “User Posts Limit”

  1. Tatiana Avatar
    Tatiana

    Hello! How to reset the user limit if the user buys not a product but a membership? Im using paid membership pro. There are three levels of membership – bronze, silver, gold.

    1. Condless Avatar

      Hi Tatiana,
      The docs was updated to include a code for PMPro

    2. Tatiana Avatar
      Tatiana

      Hi, Condless, thanks!

  2. Pedro Avatar

    Hi! I installed the plugin to use in Multisite.

    Limit configured products to 20 for Shop Manager (Woocommerce), but when I try to 21 product registration, he accuses the following error instead of talking “excedito limit.”

    Error (Warning: Creating default object from empty value in C: \ xampp \ htdocs \ experienciaonlinelojas \ wp-admin \ includes \ post.php on line 713)

    1. Condless Avatar

      Hi Pedro, please try to modify the ‘Notification’ option to Fullscreen (via the plugin settings in the subsite)

  3. Mark Avatar

    Truly awesome plugin.
    What if user has two roles. One role max 10. Another max 0. How do I display the capability (max) for only the role with the ability? Thanks!

    1. Condless Avatar

      Hi Mark,
      Please redownload the plugin and then use the code from the ‘How to display in the dashboard only the most permissive limit’ section in the docs

  4. Raul Avatar
    Raul

    Hello, it would be great if every time a limitation is created for a different user role, it would be added to a list, where said limitation rule could be edited or deleted, just like the Limit Post plugin had it, which has been outdated for 5 years. and it was fantastic.

    https://wordpress.org/plugins/limit-posts/

    Greetings and thank you very much for your contribution, you are great.

  5. Attila Avatar
    Attila

    Hi!
    How can I unrestrict multiple users in one function?
    like the first example but with multiple ids?

    Thank you very much

    1. Condless Avatar

      Hi Attila,
      The example was updated to include multiple users id.

    2. Attila Avatar
      Attila

      Thank you very much!

  6. YC Avatar
    YC

    Hi Condless,
    Thanks for creating this plugin. It’s very useful.
    I tried to use “[upl_hide type=”post”][/upl_hide]” to hide some contents, when the login-in user’s post exceeds the limit.
    However, nomatter if it exceeds the limit or not, it always hides the contents which is wrapped in.

    Any comment?

    1. Condless Avatar

      Hi,
      Can you share the website URL?
      Please try to replace “post” with the post type which the limit rule is apply on.

  7. Alfred Avatar

    Sorry about the basic question, but where do I have to copy the documentation codes for it works?

    1. Condless Avatar

      Hi Alfred,
      The code goes into your theme’s functions.php file.

  8. Copas Coding Avatar

    Can we change post type limit based on users id?

    Thanks, this is really a great plugin by the way.

    1. Condless Avatar

      Hi,
      Currently the limits can be applied per role only.

    2. Copas Coding Avatar

      Well, actually i did some workaround, by updating the option based on the user_id, altough it is not really an effective solution by using the plugin.

      Hope you can make the feature to change the limit based on the user applied on the future, and i’m ready to pay for it too, thanks for your reply.

    3. Condless Avatar

      Hi,
      Currently thats the solution, you can see an example under the section:
      ‘How to set posts limit per user?’

    4. Condless Avatar

      Hi,
      A new way to define limits per user was added (require the latest plugin version), see ‘How to define posts limit per user?’

  9. Chris Avatar
    Chris

    Is the post limit for active posts? I am using Geodirectory and want to have post limits for any custom post type (listing). So a premium user role would be limited to 10 active posts. If they have 10 posts, and delete one of these, or make it inactive, it would reduce active posts to 9, which is below their limit. This would allow them to create a new post. Possible?

    1. Condless Avatar

      Hi Chris,
      See the section: ‘How to make rules that applied on certain post type to limit specific post status?’
      You will also have to make sure the users can’t restore deleted/inactive listing.

  10. Chris Avatar
    Chris

    Can the post count include the sum of posts for all custom post types? I have 3 custom post types and want to allow a maximum number of active posts, but this could be any post type.
    #active posts = active CPT1 + active CPT2 + active CPT3
    #active posts <= post limit (set by role)

    1. Condless Avatar

      Please try the following code (update the CPT slugs):
      add_filter( 'upl_query', 'upl_multiple_post_types' );
      function upl_multiple_post_types( $args ) {
      $args['post_type'] = [ 'CPT1', 'CPT2', 'CPT3' ];
      return $args;
      }

    2. Condless Avatar

      Hi,
      An email has been sent to you with more details.

  11. Kieran Avatar

    Hello,
    I’m using an ACF Front-End form, is there anyway to make this plugin work with Front end submissions?

    Thank you πŸ™‚

    1. Condless Avatar

      Hi Kieran,
      It can work as long as the form is producing posts/pages or any other post type, try searching the post type in the ‘Type’ option (on the plugin settings).

  12. Kieran Avatar

    Thank you for the fast reply, I really appreciate it.

    The plugin is working perfectly on the backend. If I go over the post limit I get this message ” You have created the maximum amount of Review pages for this plan ”

    But, If I use the ACF Extended Front-End form, it doesn’t limit me and creates the post.

    When submitting the Front-End form, I am logged into the correct user role.

    1. Condless Avatar

      Hi, login details (preferably on dev environment) will be required to debug the issue.

    2. Kieran Avatar

      Thank you, I have emailed over the login details to a dev site.

      Thanks again πŸ™‚

  13. Abdullah YANIK Avatar

    I am using User Posts Limit plugin on my website. Anyone can register on the website and I have set a limit on the number of images they can add. No matter what I choose in the notifications section when using the plugin, it does not give the notification properly. The full screen option does not work either. How can I fix these?

    Website: finansall.com

    1. Condless Avatar

      Hi Abdullah,
      You can use any translation plugin (like Loco) or a code to translate the notification text to the desired message.

    2. Abdullah YANIK Avatar
      Abdullah YANIK

      Maybe you misunderstood me. There are no problems with translation and writing. Two of the notification options do not work when I set an image adding limit. Notifications do not appear on the image upload page.

    3. Condless Avatar

      Hi, the plugin’s built-in notifications aren’t compatible with image upload, you can translate the current message (which comes from WordPress) as per as your needs.

    4. Abdullah YANIK Avatar
      Abdullah YANIK

      Thanks for your answer. Where and how can I edit notifications?

    5. Condless Avatar

      Hi, using the above mentioned translation plugin or code.

  14. Abdullah YANIK Avatar

    Hello, while adding the media upload limit in the plugin, I chose the empty notification type. This type of notification returns ‘wp_insert_post_empty_content’ in post.php file. This warning does not comply with the media upload limit. Will you make development about it and add a warning in the plugin?

    1. Condless Avatar

      Hi,
      Please wrtie your message in the ‘text’ option, choose ’embed’ in the Notification option, and add this code into your theme’s functions.php file:
      add_filter( 'gettext', 'upl_media_notice', 10, 3 );
      function upl_media_notice( $translated_text, $untranslated_text, $domain ) {
      return is_admin() && 'Content, title, and excerpt are empty.' == $untranslated_text ? get_option( 'upl_message' ) : $translated_text;
      }

    2. Abdullah YANIK Avatar

      You did what I did by adding a function to the functions.php file. Again, the error message in post.php changes. This error message may also appear in different error states. Are you considering adding an error message field to the plugin instead?

    3. Condless Avatar

      Hi,
      You mean you need different error message for the image upload limit error? you can change this line in the code:
      get_option( 'upl_message' )
      to this:
      'Image upload limit'

  15. Marian Avatar
    Marian

    Thank you for your plugin! I use it in my site but i have one problem, I use multiple roles with multiple restriction and I need to show restrictions for the highest role or most permissive limit. But with your additional code I can see only 1 restriction even if I have it 10 for highest role. So can I somehow show for user restrictions by roles? Or all permissive limit instead of 1?

    1. Condless Avatar

      Hi Marian,
      The additional code was updated to display all the permissive limits.

Leave a Reply

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