Hide categories if no posts for that user

Posted in

Hi,

Thanks again for such a great plugin (+the categories one).

I’m trying to stop the categories being displayed for users that don’t have any posts available to them in that category.
e.g. when outputting a list of categories, we are shown the category name (with number of posts) but this doesn’t take into account the user access. It may say there are 3 posts in that category but the user doesn’t have access to any of them so I’d like to hide the category completely for that user.

Do you know of a function I could add to hide categories if no posts for that user?

Many thanks in advance
Martin

3 Responses to Hide categories if no posts for that user

  1. George October 27, 2020 at 5:05 pm #

    Hi Martin,

    Thanks for the follow-up here, your explanation makes much more sense now.
    I think for your case you should use the methods
    Groups_Restrict_Categories::delete_term_read_groups( $term_id, $group_ids )
    and/or
    Groups_Restrict_Categories::set_term_read_groups( $term_id, $group_ids )

    You should check the contents of a category and if the current user has no access to any of the posts under the category then set or delete the user’s group(s) for that term. Even though I haven’t tried it myself, it should do the trick for your case.

    Kind regards,
    George

  2. George October 26, 2020 at 4:57 pm #

    Hi Martin,

    Many thanks for your feedback, I’m glad you enjoy the plugin.
    FYI, this would be possible with Groups Restrict Categories once you restrict the category to a specific group. The category group restriction has a similar functionality like the post restriction, if a user doesn’t have access to a category then the category won’t be visible to that user.

    Kind regards,
    George

    • Martin Whittaker October 27, 2020 at 12:25 pm #

      Hi George, thank you very much for replying. Groups Restrict Categories is powerful and is suitable on a different project… but for this one there is a rather specific problem I’m trying to sort
      I’ve tried all sorts of permeations but don’t think it’s possible for what I’m trying to achieve:
      e.g.
      Users are set as different levels: User1 = Level 1, User2 = Level 3 and Level 4 etc…

      Category (CatA) has posts,
      Post1=Level 3
      Post2=Level 4
      Post3=Level 3

      In this scenario, as there are no posts in the category for this user (all posts are for either Level 3 or Level 4 users only) I would like to hide the category (CatA) for User1 until a post is added at Level 1

      I’m wondering if there is a loop I could call that checks for posts and hides the category if user has no (access) to posts in the category.

      Sorry it’s a very specific problem I’m trying to get my head around!

      Martin

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