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 by the total posts from this post type in the website and apply it on guests as well?
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_create_packages() {
	$packages = [ 'Basic Package', 'Premium Package' ];
	foreach ( $packages as $package ) {
		register_post_type( sanitize_key( $package ), [ 'labels' => [ 'name' => $package ] ] );
	}
}

function upl_add_author_support() {
	$post_types = [ 'post', 'page', 'media' ];
	if ( current_user_can( get_option( 'upl_manage_cap' ) ) ) {
		foreach ( $post_types as $post_type ) {
			add_post_type_support( $post_type, 'author' );
		}
	}
}

function upl_convert_post_types( $check, $i, $current_post_type ) {
	$packages_types = [
		'Basic Package' => [ 'post', 'page' ],
		'Premium Package' => [ 'post', 'page', 'media' ]
	];
	foreach ( $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 ) {
	$packages_types = [
		'Basic Package' => [ 'post', 'page' ],
		'Premium Package' => [ 'post', 'page', 'media' ]
	];
	foreach ( $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 (via the Users 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;
}

63 responses to “User Posts Limit”

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

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

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

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

    • 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

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

  2. 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,

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

    • 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"]

Leave a Reply

Your email address will not be published.