1

I need to list within a sidebar menu all child pages from the current parent page. The children pages are restricted for viewing by user roles, let's say "A" and "B" for now. I use the "Members Plugin" to manage that.

I've created a function that works pretty well for listing these child pages but I can't find the way to NOT LIST the child pages which the current user has no access to.

So if i'm logged in as user with role "A", I can not see the child pages which role "B" has access, and vice versa.

Here's the current code.

Any help/hint would be appreciated.

Thank you !

2
  • when you say restricting for viewing that is where? in the page summary?
    – David Lee
    Commented Feb 19, 2017 at 14:58
  • @DavidLee I use the "Members Plugin". It add post meta to pages, which I can retrieve that way. $page_role = get_post_meta($post->ID, '_members_access_role'); $user_role = sb_get_user_role(); I need to match the children page's role with the user. I'm stuck there.
    – Simon
    Commented Feb 19, 2017 at 15:07

1 Answer 1

1

If the Rol is in the meta you have to get the IDs of all of them then compare the meta access rol to the rol of the user:

    $post_parent_ID = $post->post_parent ? $post->post_parent : $post->ID;

    $title = $post->post_parent ? get_the_title( $post->post_parent ) : get_the_title( $post->ID );
    //we use get posts to get all posts for the given ID
    $children_pages_array = get_posts(array('numberposts' => -1, 'post_status' => 'publish', 'post_type' => 'any', 'post_parent' => $post_parent_ID, 'suppress_filters' => false));
    //we get the user rol
    $user_role = sb_get_user_role();
    //here we create an array that will be populated with IDs
    $ID_pages_rol_array = array();
    foreach ($children_pages_array as $child_page_obj) {
        //we get the rol that can access this page
        $page_role = get_post_meta($child_page_obj->ID, '_members_access_role');
        //do the current user rol have access to this page?
        if ($page_role == $user_role) {
            //yes it does add to the IDs of pages that this rol can access
            array_push($ID_pages_rol_array, $child_page_obj->ID);
        }
    }
    $children = wp_list_pages(array(
        'title_li' => '',
        'include' => $ID_pages_rol_array, //lets send those IDs to be formatted as list items
        'echo' => 0,
        'depth' => 1
            ));

you can get the IDs with get_posts (actually the entire post object) after making the array of permitted IDs, that can be send to wp_list_pages.

5
  • Thank you so much David ! I needed to make one change by adding [0] to the $page_role variable to make this work. $page_role = get_post_meta($child_page_obj->ID, '_members_access_role')[0]; since the sb_get_user_role(); return a string instead of an array. It wasn't matching with the $page_role array. Should we make this different or it's all good ? Again, thank you !!! You saved the day for tomorrow (Monday) I promised to provide a fix for that project. ;)
    – Simon
    Commented Feb 19, 2017 at 21:43
  • Ah I can see now, if a page grant access to user roles A & B, it won't list...
    – Simon
    Commented Feb 19, 2017 at 21:45
  • 1
    nice, i didnt know how the members plugin was doing the restriction so i put it like that, if what you have is an array, another foreach is needed to do the IF also a in_array() check so you dont have duplicate pages.
    – David Lee
    Commented Feb 19, 2017 at 21:52
  • Once finished tomorrow, I will publish the complete function for others to use.
    – Simon
    Commented Feb 19, 2017 at 21:58
  • The final working function with comments: Here's the Gist !
    – Simon
    Commented Feb 19, 2017 at 22:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.