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;
}

96 responses to “User Posts Limit”

  1. Chris Avatar
    Chris

    This is in debug.log.
    [19-Mar-2023 19:08:02 UTC] No bad record cleanup needed: 0.7.2

    1. Chris Avatar
      Chris

      Hello, any thoughts about what is causing this critical error message?

  2. Chris Avatar
    Chris

    Hello,

    I added this code snippet, and I see a field to set a maximum number of posts within the PM Pro level setup. The post type that I want to limit is a custom post type. How would I set which post type that should be limited for the membership level?

    Thank you.

    1. Chris Avatar
      Chris

      Hello,
      Is there some way to set the limit for a PM Pro membership level to limit a custom post type?

      Thank you.

    2. Condless Avatar

      The rule should be created via the ‘User Posts Limit’ plugin settings (you can select your CPT and set any limit), then the limit which configured via PMPro will override that limit based on the user membership level.

    3. Chris Avatar
      Chris

      Hello, I am unable to get this to work.

      In the User Posts Limit Settings, I select the Role = PMP Membership Role; Type = Custom Post Type; Limit = post limit (1); Cycle = None.

      In Paid Membership Pro, I went to the setup for the same membership level and enter the User Posts Limit, Limit = 1.

      I am able to submit more than 1 post of the Custom Post Type from a user that has this Membership Role. I have entered 3 posts (versus limit of 1) with no message.

      What am I missing?

    4. Condless Avatar

      Hi,
      Please contact us by Email.

Leave a Reply

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