User Posts Limit

How to make some user unrestricted in rules that applied on certain post type?
Update the user id and the post type:

add_filter( 'upl_query', 'upl_unrestrict_user' );
function upl_unrestrict_user( $args ) {
	$post_type = 'post';
	$user_id = 12;
	if ( $post_type === $args['post_type'] && $user_id === $args['author'] ) {
		$args['post_type'] = 'none';
	}
	return $args;
}

How to make rules that applied on certain post type to limit the total posts in the website?
Update the post type:

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

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

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

How to make rules that applied on certain post type to limit specific post status?
Update the post type and the post status (make sure users can’t migrate posts 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 (keep in mind that the change will not take affect in places that the date is displayed):

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 send users an Email to encourage them to upgrade their membership when they try to post after posts limit exceeded?
Attach your code for sending Email to the upl_limit_applied action hook.

How to define different message types for each user?
Use the upl_message and upl_notice filters.

How to create limit with code?
Update the role, type, limit and cycle:

$role = 'author';
$type = 'post';
$limit = '2';
$cycle = 'day';
$formatted_cycle = '1 ' . $cycle . ' ago';
$role_option = get_option( 'upl_user_role' );
$type_option = get_option( 'upl_posts_type' );
$limit_option = get_option( 'upl_num_limit' );
$period_option = get_option( 'upl_period' );
$role_option[] = $role;
$type_option[] = $type;
$limit_option[] = $limit;
$period_option[] = $formatted_cycle;
update_option( 'upl_rules_count', get_option( 'upl_rules_count' ) + 1 );
update_option( 'upl_user_role', $role_option );
update_option( 'upl_posts_type', $type_option );
update_option( 'upl_num_limit', $limit_option );
update_option( 'upl_period', $period_option );

How to modify the limits in all the rules that applied on specified user role with code?
Update the role and limit:

for ( $i = 0; $i < get_option( 'upl_rules_count' ); $i++ ) {
	$role = 'subscriber';
	$limit = '3';
	if ( $role === get_option( 'upl_user_role' )[ $i ] ) {
		$limit_option = get_option( 'upl_num_limit' );
		$limit_option[ $i ] = $limit;
		update_option( 'upl_num_limit', $limit_option );
	}
}

How to make the listing rules apply seperately on every listing type in MyListing theme?

add_filter( 'upl_query', 'upl_add_meta_query' );
function upl_add_meta_query( $args ) {
	if ( 'job_listing' === $args['post_type'] && isset( $_POST['listing_type'] ) ) {
		$args['meta_query'] = [
			[
				'key'	=> '_case27_listing_type',
				'value'	=> sanitize_text_field( $_POST['listing_type'] )
			]
		];
	}
	return $args;
}

How to display in the dashboard only the most permissive limit (relevant when users have multiple roles)?

add_filter( 'upl_current_user_limits', 'upl_display_only_highest' );
function upl_display_only_highest( $limits ) {
	foreach ( $limits as $i => $count ) {
		if ( ! isset( $max_i ) || get_option( 'upl_num_limit' )[ $max_i ] < get_option( 'upl_num_limit' )[ $i ] ) {
			$max_i = $i;
			$max_count = $count;
		}
	}
	return isset( $max_i ) ? [ $max_i => $max_count ] : $limits;
}
Posts Count Reset

How to allow posts count reset per user?

add_filter( 'upl_query', 'upl_users_cycle', 10, 2 );
add_action( 'show_user_profile', 'upl_add_cycle_user_data' );
add_action( 'edit_user_profile', 'upl_add_cycle_user_data' );
add_action( 'personal_options_update', 'upl_save_cycle_user_data' );
add_action( 'edit_user_profile_update', 'upl_save_cycle_user_data' );
add_filter( 'manage_users_columns', 'upl_cycle_user_table' );
add_filter( 'manage_users_custom_column', 'upl_cycle_user_table_row', 10, 3 );

function upl_users_cycle( $args, $i ) {
	if ( ! user_can( $args['author'], get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		$rule_role = '';
		$cycle = get_user_meta( $args['author'], 'cycle', true );
		if ( ( empty( $rule_role ) || $rule_role === get_option( 'upl_user_role' )[ $i ] ) && ! empty( $cycle ) && $cycle <= current_time( 'Y-m-d' ) ) {
			$args['date_query']['after'] = date( 'Y-m-d', strtotime( $cycle . ' - 1 day' ) );
		}
	}
	return $args;
}

function upl_add_cycle_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="cycle"><?php esc_html_e( 'Cycle', 'user-posts-limit' ); ?></label></th>
				<td><input type="date" min="1970-01-01" name="cycle" value="<?php echo esc_attr( get_user_meta( $user->ID, 'cycle', true ) ); ?>" class="regular-text" /></td>
			</tr>
		</table>
		<br>
	<?php }
}

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

function upl_cycle_user_table( $columns ) {
	$columns['cycle'] = __( 'Cycle', 'user-posts-limit' );
	return $columns;
}

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

How to reset user’s posts count when purchase some membership in Paid Memberships Pro (by the end of the day)?
Update the membership levels id:

add_action( 'pmpro_after_change_membership_level', 'upl_pmpro_update_user_cycle', 10, 2 );
function upl_pmpro_update_user_cycle( $level_id, $user_id ) {
	$membership_levels = [ '1', '2', '3' ];
	if ( ! user_can( $user_id, get_option( 'upl_manage_cap', 'manage_options' ) ) && in_array( $level_id, $membership_levels ) && ( empty( get_user_meta( $user_id, 'cycle', true ) ) || get_user_meta( $user_id, 'cycle', true ) <= current_time( 'Y-m-d' ) ) ) {
		update_user_meta( $user_id, 'cycle', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 1 day' ) ) );
	}
}

How to reset user’s posts count when purchase some product in WooCommerce (by the end of the day)?
Update the products id:

add_action( 'woocommerce_order_status_completed', 'upl_wc_update_user_cycle' );
function upl_wc_update_user_cycle( $order_id ) {
	$products_id = [ '683' ];
	$order = wc_get_order( $order_id );
	$user_id = $order->get_user_id();
	if ( ! user_can( $user_id, get_option( 'upl_manage_cap', 'manage_options' ) ) ) {
		foreach ( $order->get_items() as $item ) {
			if ( in_array( $item->get_product_id(), $products_id ) ) {
				if ( empty( get_user_meta( $user_id, 'cycle', true ) ) || get_user_meta( $user_id, 'cycle', true ) <= current_time( 'Y-m-d' ) ) {
					update_user_meta( $user_id, 'cycle', date( 'Y-m-d', strtotime( current_time( 'Y-m-d' ) . ' + 1 day' ) ) );
				}
				break;
			}
		}
	}
}

8 comments

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

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

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

Leave a comment

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