-2

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.org/plugins/bainternet-posts-creation-limits/ and https://cs.wordpress.org/plugins/wpsite-limit-posts/ but they dont work.

Thanks!

  • 2
    Please read How to Ask. – kaiser Jun 8 '15 at 19:01
  • By default, subscribers can't create posts at all so, first off, how was that hacked into place? – s_ha_dum Jun 8 '15 at 19:18
4

This 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 );
  • how to customize it so that it works for the subscriber and for custom post type? thank you very much – Vale Boccaccio Jun 29 '15 at 8:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.