Skip to:

Getting parent category and filtering against a query

  • Hi, I’m integrating bbPress with s2member and need to restrict access to certain categories, forums and their topics.

    For example, here’s a hierarchy:

    Home > Forums > Category1 > Subcategory1 > Forum1 > Subforum1 > Topic1

    If someone tries to look at Topic1 or any of its parents, I need to check to make sure they have access to Category1.

    Access is managed by s2member.

    For typical wordpress posts, I attach this filter, run a wp_query, then detach it:

    attach_s2member_query_filters(); // only fetch content the user can access

    If that query is not empty, then I let the user view the page they’re trying to look at. If it is empty, it means they don’t have access to any posts and so I redirect them.

    But I don’t know how bbPress works well enough to figure it out.

    It seems I need to

    1. Find what the root category is

    2. Check if the user can access the root category

    3. If so, allow access to whatever they’re trying to see. If not, redirect or give an error message.

    Any ideas on how I can do this?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Happy to pay someone for their working solution.


    I don’t have any experience with s2member or for using forums that are that in depth (cat > subcat > forum > subform ) so I can’t really say the best route to go.

    For something like this, have you considered using BuddyPress?

    That would allow you to setup different groups (which have forums within them) and configure different permissions to each one, etc.

    I considered Buddypress, but it’s way more robust than what I’m needing, and installing it was breaking my custom theme (jquery conflict somewhere).

    Thanks for looking at this.

    For anyone else out there, here’s my solution for restricting bbPress access using s2member. In short, this checks if the current user has access to the most distant ancestor of the current page. For example:

    Level 1 Forum/Category > Subforum1 > Subcategory > Subforum2 > Current Topic

    No matter which page they arrive at in the tree, they must have access via s2member to the top-most level. Then it’s a simple matter of assigning a ccap to that Forum, or a membership level.

    1. Include the following in your functions.php file

    2. Include a call to the function in EACH of your bbPress template files BEFORE the template calls for the forum content. Match the HTML structure of the error message to suit your theme. You need to include closing tags for any open divs.

    3. Test!

    /******* bbPress Forum Access *******/

    function checkForumAccess() {

    if(get_post_type() == 'forum' OR get_post_type() == 'topic' OR get_post_type() == 'reply') { // check if user is viewing a forum page. If so, continue.

    //check if they have access to the top-most parent forum/category (both are Pages in wordpress). If not, show the error verbiage. If they have access, continue and show the content like normal

    $parent = array_reverse(get_post_ancestors($post->ID)); // get an array of all the parent pages in order from most distant to closest.

    $first_parent = get_page($parent[0]); //get the first page in the array, which is the most distant parent

    $int = apply_filters(‘the_ID’, $first_parent->ID); //filter the $first_parent to get only the wordpress page/post ID, which is an integer like 49 or 565. store it as a variable $int

    if (!is_page_permitted_by_s2member($int)) { // this is an s2member function. Check if the current user can access the $first_parent, via the stored page/post ID in the $int variable

    // here comes your custom error message. Remember to escape any apostrophes

    echo ‘


    <h1 class=”entry-title”>Sorry!</h1>

    <div class=”entry-content”>

    <p>It looks like you don’t have access to this forum.</p>

    <p>If this is an error and you should have access, please contact us here: (contact URL)



    </div><!– #content –>

    </div><!– #contentWrapper –>

    </div><!– #primary –>




    exit; // Important. This prevents the rest of the page (the secure forum content) from displaying






    Thanks for looking at this, jaredatch. Buddypress was overkill for what I needed and was also breaking my custom theme. Some jquery conflict I didn’t want to find.

    For any others out there, I integrated bbPress with s2member permissions successfully. I posted my solution on the s2member forums:

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.
Skip to toolbar