User Posts Limit

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

add_filter( 'upl_query', 'upl_unrestrict_users' );
function upl_unrestrict_users( $args ) {
	$post_type = 'post';
	$users_id = [ '12', '15' ];
	if ( $post_type === $args['post_type'] && in_array( $args['author'], $users_id ) ) {
		$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 from/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 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 (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 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;
}

How to set posts limit per user?
Update the rule number, the users id and the limits (compatible only if users create posts only for themself, not compatible with the Document Statistics option):

add_filter( 'option_upl_num_limit', 'upl_limit_per_user' );
function upl_limit_per_user( $value ) {
	$rule = '1';
	$limits = [
		'12'	=> '5',
		'15'	=> '6',
	];
	if ( isset( $limits[ get_current_user_id() ] ) ) {
		$value[ $rule - 1 ] = $limits[ get_current_user_id() ];
	}
	return $value;
}
Integrations

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

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

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

    Thank you very much

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

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

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

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

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

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

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

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

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

Leave a Reply to Condless Cancel reply

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