• Resolved wendath

    (@wendath)


    Hello,

    I am trying to limit the maximum number of published posts for a custom post type. Lets say I want to limit the total to 50 posts, but not per user role.
    I only want to limit published post, so I want to exclude drafts.

    Do you guys have any idea how to achieve this?

    Thanks in advance!

    • This topic was modified 4 years, 11 months ago by wendath.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    You can do a COUNT query for posts of that type where the status is “publish” only. If it’s >50 kill the process. Do so from any action that fires when the post is saved or its edit screen loaded. Killing the process is crude but effective. Not the best UX. You could try to hide any new post links and buttons with CSS so users are less likely to encounter the process kill, but that can cause confusion too.

    It’s possible instead to put up a message box similar to the “Post saved” message on the edit screen. As usual, the best UX takes the most work.

    Thread Starter wendath

    (@wendath)

    Thanks for your answer!
    Yeah, that sounds like a possible solution, however I don’t really know to code it as I am not that experienced with PHP. I inspected some code which restricts a specific user role, but this seems too advanced for me.

    Thread Starter wendath

    (@wendath)

    Okay, so I solved it!
    Actually, I have two solutions for this.

    1st solution (easier)
    —————-
    I hid the “Publish” (not the save) button with jquery, and added an error notice if the post count was above ‘x’.

    2nd solution
    —————-
    I modified the ‘Limit Posts’ plugin, so now I can restrict “ALL” instead of just a specific user/user role.

    @wendath hi, i’m looking for similar solution too. Would you mind to share with me the edited plugin please? i’m not a coder and the plugin has been updated for too long…please share github link or anywhere i can get it. thanks so much

    Thread Starter wendath

    (@wendath)

    Hey @archonic08,
    This is what I came up with. This goes into your child theme’s functions.php

    Have fun!

    https://github.com/wendath/sprout/blob/master/Limit%20posts.php

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limiting maximum number of posts published per custom post type’ is closed to new replies.