Hide categories if no posts for that user

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

Posted in

Comments

3 responses to “Hide categories if no posts for that user”

  1. 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. 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

    1. Martin Whittaker Avatar
      Martin Whittaker

      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

Share