Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

Restrict Access to Posts based on Custom User and Post Meta Data

In a similar use case scenario, I had a client with 5 locations which could be assigned to the user. This will need to be changed for you because it’s based on location being assigned to both a user and a post, whereas you’re just going to assigning entire posts to users rather than matching post meta.

function filter_by_user_club( $query ) {
  if( is_admin() ) {
    $user_id        = get_current_user_id();
    $user_location  = get_user_meta( $user_id, 'myplugin_user_location', true );
    $currentscreen  = get_current_screen();
    if ( $currentscreen->post_type == 'custom_post_type' ) {
        if( !empty( $user_location ) ) {
            $location_meta_query = array(
                'key'   => 'cpt_location',
                'value' => $user_location,
                'compare'   => 'IN'
            );
            $query->set('meta_query', array( $location_meta_query ) );
        }
    }
  }
add_action( 'pre_get_posts', 'filter_by_user_club', 9998 );

I’m guessing that this sort of modification would need to be applied for your method, if I’m understanding your structure correctly…

You would need to change the meta_query to instead query an array of IDs using post_in, like so:

    function filter_by_user_club( $query ) {
      if( is_admin() ) {
        $user_id        = get_current_user_id();
        $user_clubs     = get_user_meta( $user_id, 'myplugin_user_clubs', true );
        $currentscreen  = get_current_screen();
        if ( $currentscreen->post_type == 'club_post_type' ) {
            if( !empty( $user_clubs ) ) {
                //if you've saved the user club IDs as an array (recommended)
                $query->set('post_in', $user_clubs );
                //if you've saved the user club IDs as a string use this...
                //$query->set('post_in', array( $user_clubs ) );
            }
        }
      }
    add_action( 'pre_get_posts', 'filter_by_user_club', 9998 );

Obviously a lot still to do to get the club IDs associated with the user, etc. but this will filter the club post type list based on the user.

We’re using $query->set() and essentially adding an argument into that screen’s default get_posts() query. While the query here, by default, will grab all of the posts (paginated based on how many you set per screen), we’re now telling it to only show posts_in the array we provided when we checked which clubs a user has associated with them.

Related Posts:

  1. Possible to hide Custom Post Type UI/Menu from specific User Roles?
  2. Custom post type role permissions won’t let me read
  3. Allowing custom role access to custom post type in back end
  4. How to assign specific users the capability to edit specific pages / posts / custom post types
  5. Allow user to Edit Posts but not Add New?
  6. Defining capabilities for custom post type
  7. Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter
  8. Can I make user role that can only access a certian content type?
  9. Create user role restricted to specific CPT
  10. How to set individual capabilities on a taxonomy? Or how to re-register an existing taxonomy?
  11. Add Capabilities to Custom Post Type after it has been created [duplicate]
  12. How to not allow custom roles to edit published custom post types?
  13. Creating a custom post type upon registration
  14. How to restrict specific post types from being read or added by specific user roles (eg. author)?
  15. How do I code access to the built-in UI of a CPT when it’s placed as submenu of another CPT that is protected by role?
  16. Prevent author role from editing all posts in custom post type?
  17. How to allow “Add New” capability of CPT when links to its UI are placed as a submenu?
  18. Custom Role can’t trash Custom Post Type
  19. Limit access to page depending on user level
  20. Role Capabilities: Add New Ones?
  21. Custom post type capabilities require “create_posts” to access the edit posts list page
  22. Add custom capabilities to existing custom post type
  23. allow edit of custom post type but not regular posts?
  24. Allow Administrator role access to custom capabilities [duplicate]
  25. Why is my Custom Post Type not showing up after adding capabilities?
  26. Cannot attach media when capabilities added to custom post type
  27. How to restrict CPT post’s fronted view only for specific user roles?
  28. how to delete all users and posts based on ‘user_meta’?
  29. current_user_can() return FALSE but debugging says TRUE
  30. Role capability delete multiple post type posts doesn’t work
  31. map_meta_cap woes
  32. Roles for Custom Post Types
  33. can’t see custom post content filtered under “mine” filter in admin panel
  34. Define new user capability for custom post types?
  35. Custom post types as sub menu pages and role capabilities issue
  36. Only view/edit/delete CPT made by users with the same role
  37. How to set individual capability of post type in individual category
  38. Giving permission to anyone (non-users as well) with a password to edit a post, possible?
  39. WooCommerce Customer Role Delete Custom Post Type
  40. Users create/join groups
  41. Disable user from updating certain posts
  42. “Submit for review” for updates on existing posts
  43. Access to CPT but not to ‘post’ post type
  44. Allow add new post access to custom post but not wp post for some role
  45. Allow user to only access custom post type
  46. WordPress custom post type capabilities issue
  47. Role capabilities issue
  48. WordPress: Custom User Role cannot access Custom Post Type | “Sorry, you are not allowed to access this page”
  49. Select other roles as custom post authors
  50. How to handle this specific case of custom post type?
  51. Meta box with front-end styling
  52. Roles and Capabilities in Custom Post Types
  53. How to fix the Post Preview Button (CPT & map_meta_cap)
  54. Restrict Custom Post Type per role in Dashboard
  55. Restrict access to custom post type based on its taxonomy terms
  56. Why “Mine” is the default view when adding ‘capability_type’ in register_post_type
  57. Conditional editing CPT – using editor’s role and author’s usermeta
  58. How to only display all posts to a custom User Role?
  59. Creating a custom post type upon registration for a specific user role
  60. Allowing custom role user to edit post assigned to them but don’t let them create new custom type post
  61. Multiple useres editing specified content
  62. WooThemes – Vendors / Bookings – Allow Vendors to manage resources
  63. Capibilities of CPT WordPress
  64. How can I remove “Add new” button on custom post type
  65. Capabilities and mapping required for a role to be able to edit other’s posts of a custom type, BUT only be able to edit their own blog posts
  66. Weird capabilities / roles behavior
  67. Prevent author role from editing others posts
  68. creating different edit screens for different roles
  69. Display a post count from a custom metabox selection
  70. Display and Allow users to edit their own profiles
  71. Capability to read user’s own draft post of CPT
  72. Custom role, capabilities, and post type: preview button wrecks things
  73. Custom post type & role issues
  74. Read-Only custom post type
  75. How to display Author Profile based on Custom field value
  76. Need some suggestions with Relationship fields and CPT/ACF
  77. Allowing a CPT post to be edited by a single user role
  78. Update postmeta Parent when post_status child change
  79. Allow Contributor to edit but not update a published post
  80. Metabox not show in categories custom post type cmb2
  81. WordPress request fiter order by related post’s post_title
  82. Edit the author of custom post type
  83. Update database from Quick Edit according to Checkbox state of Custom Post Type
  84. Custom fields (wp_post_meta) vs Custom Table for large amount of data
  85. Deny user access to edit post while allow him to edit custom post type
  86. Storing a many to many post type relationship in post meta and keeping SQL ability for Joins
  87. Dynamic Custom Fields
  88. Show Custom Post Type meta boxes only on Page Edit
  89. Query posts based on the meta key values of logged-in users?
  90. How to restrict author to only access one custom post type ?
  91. Cannot Create new Post within Custom Post Type
  92. Create custom post with custom user rules
  93. Custom post type submenu capabilities – custom plugin
  94. Custom filter function not working with Custom post type
  95. How to show featured image block in custom post type for Author?
  96. re-register custom post type with custom capabilities
  97. Importing Data from a Non-WordPress database, into WP
  98. Getting thumbnails of a custom posts featured images in a page-templates metabox
  99. Want to be able to sign up subscribers as authors
  100. Organize WordPress site, so it can maintain with huge database
Categories custom-post-types Tags capabilities, custom-post-types, post-meta, user-meta, user-roles
How to dynamically populate a dropdown
Removing post category base giving Error 404

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress