Hi my wordpress users are able to submit posts. But I want to limit the number of posts that they can submit. Can you help me?
I already tried / and / but they dont work.
Thanks!
Hi my wordpress users are able to submit posts. But I want to limit the number of posts that they can submit. Can you help me?
I already tried https://wordpress/plugins/bainternet-posts-creation-limits/ and https://cs.wordpress/plugins/wpsite-limit-posts/ but they dont work.
Thanks!
Share Improve this question asked Jun 8, 2015 at 18:49 Vale BoccaccioVale Boccaccio 1 2- 2 Please read How to Ask. – kaiser Commented Jun 8, 2015 at 19:01
- By default, subscribers can't create posts at all so, first off, how was that hacked into place? – s_ha_dum Commented Jun 8, 2015 at 19:18
1 Answer
Reset to default 5This solution uses the publish post hook. What's happening here is that when a post is published, it runs the custom post_published_limit
function we create below.
The $max_posts is set statically here, you can expand it to pull the value from elsewhere, but that's beyond the scope of the question being asked. However there is plenty of literature describing how to do that.
The function then gets the count of posts of the author currently trying to publish.
If the $count exceeds the $max_posts the function will save it as a draft instead of publishing it.
function post_published_limit( $ID, $post ) {
$max_posts = 5; // change this or set it as an option that you can retrieve.
$author = $post->post_author; // Post author ID.
$count = count_user_posts( $author, 'post'); // get author post count
if ( $count > $max_posts ) {
// count too high, let's set it to draft.
$post->post_status = 'draft';
wp_update_post( $post);
}
}
add_action( 'publish_post', 'post_published_limit', 10, 2 );