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_$i", 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">
		<?php for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) { ?>
			<tr>
				<th><label for="num_limit_<?php echo $i; ?>"><?php esc_html_e( 'Rule', 'user-posts-limit' ); echo $i + 1 . ' '; esc_html_e( 'Limit', 'user-posts-limit' ); ?></label></th>
				<td>
					<input type="number" min="0" name="num_limit_<?php echo $i; ?>" value="<?php echo esc_attr( get_user_meta( $user->ID, "num_limit_$i", true ) ); ?>" class="regular-text" />
					<p class="description"><?php esc_html_e( 'It will override the configured limit for this user on rule', 'user-posts-limit' ); echo $i + 1; ?></p>
				</td>
			</tr>
		<?php } ?>
		</table>
		<br>
		<?php
	}
}

function upl_save_num_limit_user_data( $user_id ) {
	if ( current_user_can( get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) {
			update_user_meta( $user_id, "num_limit_$i", sanitize_text_field( $_POST["num_limit_$i"] ) );
		}
	}
}

function upl_num_limit_user_table( $columns ) {
	for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) {
		$columns["num_limit_$i"] = __( 'Rule', 'user-posts-limit' ) . ( $i + 1 ) . ' ' . __( 'Limit', 'user-posts-limit' );
	}
	return $columns;
}

function upl_num_limit_user_table_row( $row_output, $column_id_attr, $user_id ) {
	for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) {
		if ( "num_limit_$i" === $column_id_attr ) {
			return get_user_meta( $user_id, "num_limit_$i", true );
		}
	}
	return $row_output;
}

How to set different limit message per user role?
Update the user role and its message:

add_filter( 'upl_message', 'upl_message_per_role', 10, 2 );
function upl_message_per_role( $message, $postarr ) {
	$message_per_role = [ // Sort roles from strong to weak, first match will be applied
		'author'		=> 'message1',
		'editor'		=> 'message2',
	];
	foreach ( $message_per_role as $role => $role_message ) {
		if ( user_can( $postarr['post_author'], $role ) ) {
			return $role_message;
		}
	}
	return $message;
}

111 responses to “User Posts Limit”

  1. Louis Avatar
    Louis

    Bonjour,

    Voici ma problématique.

    J’ai un formulaire qui permet à l’utilisateur de créer un post qui obtient le statut “En attente de relecture”.

    Si celui-ci est validé (par l’administrateur), il passe alors en publié. Le problème est le suivant : l’utilisateur peut décider de dépublier son article qui passe dans avec le statut “draft”. Cependant, il peut également décider de le republier avec le statut “publish”.

    Mon problème est donc que cela ne fait pas référence à une création de post mais à un changement de statut. Est-il possible de simplement limiter la création de posts, et la mise à jour du staut a “publish” ?

    Merci pour votre aide et votre merveilleux plugin.

    1. Condless Avatar

      Hi Louis,
      The user ‘republish’ the post via the frontend or the backend?
      Try using the code from ‘How to make rules that applied on certain post type to limit specific post statuses?’ section to limit only ‘publish’ posts and wrap the Publish button of your form with the [upl_hide] shortcode, so the button will be hidden once the user excceed the published posts limit.

    2. Louis Avatar
      Louis

      The user republishes the article from the front-end via a form from the Crocoblock plugin: JetFormBuilder.

      The problem is that I am not able to hide the button with the shortcode.

      Also, I tried to display the shortcode announcing the number of posts the user is able to publish on the front-end, but it doesn’t work.

    3. Condless Avatar

      Hi,
      Insert 2 Shortcode widgets using Elementor, before the button:
      [upl_hide type="post"][/upl_start]
      after the button:
      [/upl_hide]

  2. Maria Avatar

    Hello,
    Is it possible to notify a user on the frontend that the post limit has been exceeded?
    Thank you

    1. Condless Avatar

      Hi Maria,
      Try the shortcode (replace “post” with the relevant post type):
      [upl_hide type="post"]

    2. Maria Avatar

      Hello,

      I have added the shortcode [upl_hide type=”videos”] but it doesn’t show anything to the user on the front page.

      I have a limit so any user with the “subscriber” role cannot upload more than ten videos and it works.The problem is that it doesn’t notify you that you have exceeded the limit.

      Thank you a lot

    3. Condless Avatar

      Hi Maria,
      Try “video” instead “videos”.

    4. Maria Avatar

      Hello,
      Yes, yesterday I tried both options but it was still the same

    5. Condless Avatar

      Hi,
      Insert 2 Shortcode widgets using Elementor, the first contains:
      [upl_hide type="video"][/upl_start]
      and the second contains:
      [/upl_hide]

  3. Unakriti Avatar
    Unakriti

    Thank you so much for this plugin.

    I am trying to control for a CPT made with Metabox.io. But it does not work – the restricted user-role can continue to create more posts. There are no warnings / limit notifications shown either.

    What am I missing?

    Really appreciate this plugin and sincerely hope I can make it work to my scenario.

    Kind regards,

    1. Condless Avatar

      Hi,
      The plugin should work on CPT as well, a detailed answer was given by Email.

  4. joko Avatar
    joko

    how integrate with plugin membership, any hooks and filter doc? thanks

    1. Condless Avatar

      Hi Joko,
      Integrations are available for WooCommerce, Paid Membership Pro, MyListing theme, Restrict Content Pro. Which membership plugin do you use?

  5. Fachrul Hasan Avatar
    Fachrul Hasan

    Hello, how can I show how much time left customer has on the front end?

    1. Condless Avatar

      Hi Fachrul,
      In the ‘text’ option (via the plugin settings) insert ‘{release_date}’, then in the relevant page use the shortcode (replace “post” with the relevant post type): [upl_hide type="post"]

  6. אהרן כהן Avatar
    אהרן כהן

    Hello, is it possible to separate the number of posts the user has published and the number of posts he can publish so that the shortcode will not display both

    1. Condless Avatar

      Hi Aaron,
      You can add the ‘format’ attribute to the shortcode as follow:
      [upl_limits format= "{type} limit: {limit}. "]

    2. אהרן כהן Avatar
      אהרן כהן

      Hi thank you very much you helped me a lot only shortcode shows only how much I can post
      I also need a shortcode of how much the user posted

    3. Condless Avatar

      You can use {left} or {count} instead of {limit}

  7. MJ Avatar
    MJ

    I used your plugin and a problem occurred~

    When the set condition is triggered, a wordpress error will pop up

    How can I solve this problem?

    If you receive a message, I hope you can reply me~

    1. Condless Avatar

      Hi MJ,
      Users submit posts using the admin dashboard or using some plugin? can you provide link to the website?

  8. MJ Avatar
    MJ

    Hello,Condless~

    I emailed you~

    In the email, there are many pictures~

  9. MJ Avatar
    MJ

    Hello! I have solved this problem!

    On line 576 of the code:

    wp_ die( $prepared_message ,”, [ ‘back_link’ => true ] );

    There is no content in ”

    So a wordpress error is displayed

    So I changed it to:

    wp_ die( $prepared_message ,’ Prompt’, [ ‘back_link’ => true ] );

    And solved the problem

    Very good plug-in, it helped me solve the problem

    Thank you.

  10. Alex Avatar

    Hello,
    Could you please update the section on How to allow a post limit per user (will be displayed on the Edit User Screen)?

    When I add the code to the function.php file, it gives me an error on line 42 (unexpected ‘}’).
    Any help, please?
    Thanks!

  11. Alex Avatar

    And when I added via the FTP, the website stopped working giving me a fatal error!
    I restored it, but I can’t figure out how to add this functionality (limit per user)!

    1. Alex Avatar

      the code of this section I mean:
      How to allow posts limit per user (will be displayed on the Edit User Screen)?

    2. Condless Avatar

      Hi Alex,
      Please enable the debug log and check which error displayed there.

    3. Alex Avatar

      When I try to add the code to the theme editor, it says error, unexpected ‘}’ on line 42.
      Please try to update the code of the section on how to limit posts per user.
      Thanks.

    4. Condless Avatar

      Hi,
      The code snippet seems OK, please make sure you copied the whole code.

    5. Alex Avatar

      Hi,
      Could you please try to add the code snippet on a website and see if it works?
      I can’t get it right! There is always a fatal error when I add it using the SFTP and an error when using the theme editor.
      Thanks in advance!

    6. Condless Avatar

      Hi,
      Please send us by email the code snippet you have used.

    7. Alex Avatar

      Or, is there any chance you can update your great plugin with this feature built in the plugin without the need of adding it via code snippet?
      I am referring to limit post per user.
      Thanks!

    8. Alex Avatar

      sent you an email.
      Thanks!

  12. Ryan Scott Avatar

    how can we find out the date at which it re-ups its quota?

    1. Condless Avatar

      Hi Ryan,
      Code snippet was sent to you by Email.

  13. Fabio Avatar
    Fabio

    Hi,
    I am using your plugin for the first time.
    I would like to use it to limit to one Custom Post “Member” to users with Contributor role.
    Are the plugin settings enough or should I add some code?

    1. Condless Avatar

      Hi Fabio,
      Plugin settings are enough, Select ‘Contributer’ in role and ‘Member’ in type field.

  14. Chris Avatar
    Chris

    Hi,

    Where is the “docs” with instructions that is mentioned? I see a comment about integration with PM Pro and do not see anything about this.

    1. Condless Avatar

      Hi Chris,
      https://en.condless.com/wp-content/uploads/pmpro-upl-integration
      Add this code snippet into your theme’s functions.php file, then you’ll be able to set limit per membership level.

  15. Chris Avatar
    Chris

    Hello,

    I have used User Posts Limit successfully in the past. I am now testing again.

    This is a staging site, and I have tested here before.

    I have entered a condition to limit the number of posts to 1 for a specific user role.

    When I try to enter the second post, I get a critical error message, and do not get the pop up text message.

    I have disabled other plugins, tried another theme, and cleared cache. Nothing has helped.

    Any ideas on how to correct this?

    1. Condless Avatar

      Hi Chris,
      Which ‘critical error message’ did you get? can you check also your email or the website debug log?

Leave a Reply

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