Change WordPress user roles and capabilities Forums How to or FAQ Access to all Posts of a specific Custom Post Type and limit other Pages by ID

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #6724
    edering
    Participant

    Hi. I have a Role called Applications & Tools which gives the User access to manage (abilities – https://www.screencast.com/t/qnd9LiKdddq ) all the Posts in that Custom Post Type. I have another Role called SDoL Page Editor that lets users Delete and Edit (abilities – https://www.screencast.com/t/8H9XuQte2br ). When we use SDol Page Editor, we use your Access Restrictions at the User level by Post ID. But we have two users that we need to give access to both Roles, and set access to one specific Page. It looks like when I try this, https://www.screencast.com/t/tSSVWvPXZ , none of the Posts in our Applications & Tools Custom Post Type appear, and also no Pages (not even the page with the ID we set) shows. I looked at the Admin Menu settings for both Roles. SDoL Page Editor: https://www.screencast.com/t/soZdth5xeZm and Applications & Tools: screen 1 – https://www.screencast.com/t/oid3nMK1iq screen 2 – https://www.screencast.com/t/p0oqztvEC . I was thinking maybe Pages was being blocked from the Applications & Tools role, but it does not even show as an option (but is set to show of course when only SDoL Page Editor Role is used). Any ideas how to get a User access to one Custom Post Type, but add additional Pages/Posts with a different role’s right?

    #6725
    Vladimir
    Keymaster

    Hi,

    1st, deactivate temporally URE’s edit restrictions add-on at the URE Settings page and be sure that your user with both roles really has unrestricted access to the custom post type and pages.
    2nd, turn ON URE’s edit restrictions add-on and try to tune restrictions for this user.
    When you put page ID to the list of allowed for editing, URE automatically prohibits all other ID (even from other post types). It’s possible to change this for the selected custom post type via ure_restrict_edit_post_type custom filter.

    Let me know the result and we will continue with access to the single page, if it will be needed.

    #6726
    edering
    Participant

    For your first step, disabling the restrictions add-on does show all Posts in my specific Custom Post Type for that Role. But no pages show for the SDoL Page Editor Role at all. What would cause that? I don’t think Pages is being blocked in the Admin Menu (if you look at my links in my first post).

    #6728
    Vladimir
    Keymaster

    Does such user see ‘Pages’ menu item or just doesn’t see any pages at the pages list?
    Anyway I don’t see a problem with user capabilities granted to the user via roles.
    Try to deactivate all plugins. Will it change something? If Yes, activate plugins back one by one to isolate a problem.

    #6730
    edering
    Participant

    The user does not see any Pages menu item at all. I cloned Production to Staging and then disabled all plugins except URE. When testing a user as described above, now I don’t see any menu items except for Profile (not even the Applications & Tools, which I saw before). What would cause that? I had already given you access to this site before. You can access it at https://staging-sdlancaster.kinsta.cloud/ with userroleeditor (you can reset your password). I have enabled all Plugins again (except for 4 that are also deactivated on Production). I also removed the Post ID from the restrictions of the User called “icomm” (my test user). This is the user that should use the Applications & Tools Role (full access to that Custom Post Type) plus SDoL Page Editor role access for the post ID=28499. If you could look at that and test with icomm, I would appreciate it.

    #6731
    Vladimir
    Keymaster

    1st issue was resolved. User icomm sees ‘Pages’ menu now. Problem was that his 1st role has “Admin menu” “Not Selected” blocking model, but the 2nd role – “Selected”. When URE builds general admin menu for 2 roles it take blocking model from the 1st role and if blocking model of the 2nd role is different then it’s ignored. As a result ‘Pages’ menu was blocked via “Admin menu” restrictions set for the 1st role.

    2nd part, it’s possible to allow user to see/edit the single page via its ID, but has edit access to the full list of another custom post type. Use custom filter ure_restrict_edit_post_type for this purpose.

    Let me know if you will need help with a code for this custom filter.

    #6732
    edering
    Participant

    Thanks! For the 2nd part, our SDoL Page Editor role ID is called sdol-page-editor. Would I use that for the Role, AND set the post_type to “post”? Or, is the idea to list the Role ID for Applications & Tools (applications_tools)? And do I list the post_type for the Applications & Tools custom post type?

    #6733
    edering
    Participant

    Also, we already have this filter being used once for a different role/custom post type. How do we add it again, but make sure they run independently? See below.

    // URE to allow Scholarship Role access to Scholarship plugin while restricting Pages for user
    add_filter('ure_restrict_edit_post_type', 'exclude_posts_from_edit_restrictions', 10, 1);
    function exclude_posts_from_edit_restrictions($post_type) {
      $restrict_it =  $post_type;
      $user = wp_get_current_user();
      if ( empty( $user) || !is_array( $user->roles ) ) {
          return $restrict_it;
      }
      if ( in_array( 'scholarships', $user->roles ) ) {  // Role ID
          if ($post_type=='scholarship') {  // CPT ID
            $restrict_it = false;
          }
      }
      return $restrict_it;
    }
    // end URE to allow Scholarship Role access
    #6734
    Vladimir
    Keymaster

    Use ‘sdol-page-editor’ for the role ID and ‘app_tools’ for the post_type. If you return false for ‘app_tools’, URE will fully exclude this custom post type from edit restrictions.

    Something like this:

    
    if ( in_array( 'sdol-page-editor', $user->roles ) && in_array( 'applications_tools', $user->roles )) {  // user has both roles at the same time
          if ( $post_type==='app_tools' ) {
    	   $restrict_it = false;  // Do not restrict editing for Applications & Tools
          }
      }
    
    #6735
    Vladimir
    Keymaster

    This version just adds additional condition:

    
    if ( in_array( 'scholarships', $user->roles ) ) {  // Role ID
          if ($post_type=='scholarship') {  // CPT ID
            $restrict_it = false;
          }
    } else if ( in_array( 'sdol-page-editor', $user->roles ) && in_array( 'applications_tools', $user->roles )) {  // user has both roles at the same time
          if ( $post_type==='app_tools' ) {
    	   $restrict_it = false;  // Do not restrict editing for Applications & Tools
          }
      }
    
    #6736
    edering
    Participant

    Thanks!

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.