How to automatically protect a custom post type with Groups

Posted in

I’ve got a custom post type I always want to protect (with the group ID 2). I’ve tried to write a function to automatically add this group protection on post_save and even got ChatGPT to try a few times and it doesn’t seem to work. I just purchased the Groups Restrict Categories add on as that looked like it might work (as it mentions custom post types) but it doesn’t do what I need.

I could protect a category and automatically add that category to the custom post type on post_save but would rather not use extra categories or risk that category being removed and unprotecting the content.

Is this possible with groups and if so, could you point me to an example or a particular function I could use?

Thanks

4 Responses to How to automatically protect a custom post type with Groups

  1. George March 3, 2023 at 6:11 pm #

    Hi Patrick,

    That’s good so next, you can hook to save post action and add the desired group restriction. I’m not quite sure about the exact hook name, but there is one that triggers on post-save.
    As for adding the access restriction, you can use
    Groups_Post_Access::create($map)
    and add your group restriction.
    If further assistance is needed on this part, please let me know. I’ve also taken a note on the heading typo of Groups > Options, many thanks for that.

    Kind regards,
    George

  2. Patrick Hazard March 3, 2023 at 12:41 am #

    Also while scouring the options to see if I was missing anything I noticed that restrictions was spelt incorrectly. See link

    https://share.cleanshot.com/sWYfBfws

  3. Patrick Hazard March 3, 2023 at 12:39 am #

    Hi George, thanks for that. I’ve looked on the options page and the post type options just seem to let me let these post types be protected – what I want to do is automatically protect all of a particular post type with a certain group. Unless I’m missing anything I can’t see how to do this in the options so was just going to set this on post save in my plugin.

    Thanks

  4. George March 2, 2023 at 10:28 pm #

    Hi Patrick,

    Welcome back to our support forum.

    In general with Groups you can protect the standard post types but also custom post types. If you go to Groups > Options then normally you should be able to select your post type from the list.
    If your post type is not listed there, then it’s also possible that something’s wrong with the post type itself. We can offer to have a look should you choose to share your code with us.

    Kind regards,
    George

We use cookies to optimize your experience on our site and assume you're OK with that if you stay.
OK, hide this message.

Affiliates · Contact · Jobs · Terms & Conditions · Privacy Policy · Documentation · Downloads · Useful Plugins · My Account

Share