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

Assign a Post to a User

As Gael mentioned, there’s not native WordPress functionality to handle this, but it’s not too difficult to achieve.

update_post_meta( $post_id, 'post_subscriber', $user_id );

First, I would create post meta that identifies the user who should have access. You would probably want to set this up with Meta Boxes on the individual pages or using the Settings API. If you have multiple users that need access to the same page, you may want to set this up as User Meta instead which would flip the code below a bit on its head.

Since we’re doing 1 user accesses 1 page, the post meta should work fine.

Next, you need to determine whether you want to hide the content of the page only or pretend the page doesn’t exist at all. Pick one of these two, not both.

1. Hide the content

We’ll filter the page’s content, leaving everything else accessible (like the title, featured image, etc. You can replace the content with a message informing the visitor that they’re not allowed to see this content, “Nope!” It’s a good practice to include a login form along with your message in case the user just forgot to log in ahead of time.

function my_filter_content( $content ) {
    global $post;
    if ( empty( $post ) || ! is_page() ) {
        return $content;
    }

    $post_subscriber_id = get_post_meta( $post->ID, 'post_subscriber', true );
    if ( ! $post_subscriber_id ) {
        return $content;
    }
    $user = wp_get_current_user();
    if ( $user->ID === $post_subscriber_id || user_can( $user->ID, 'administrator' ) ) {
        return $content;
    } else {
        // Content restricted message.
        return 'Nope!';
    }
}
add_filter( 'the_content', 'my_filter_content', 11 );

2. Redirect the page

Instead of allowing anything to be seen by unauthorized users or the public, this action redirects unauthorized users to a completely separate page where they can be informed they tried to access something by mistake. It’s usually a good practice to include a login form on this page in case the user has access, but forgot to log in.

function my_page_template_redirect() {
    // Conditions for targeting the correct content type.
    if ( is_page() ) {
        global $post;
        $post_subscriber_id = get_post_meta( $post->ID, 'post_subscriber', true );
        // Now we know this page has restricted content.
        if ( $post_subscriber_id ) {
            // Check user is logged in.
            if ( is_user_logged_in() ) {
                $user = wp_get_current_user();
                // Check user is allowed access.
                if ( $user->ID === $post_subscriber_id || user_can( $user->ID, 'administrator' ) ) {
                    return;
                }
            }
            // Redirect user to page explaining why they can't see the content.
            wp_safe_redirect( home_url( '/access-denied/' ) );
            die;
        }
    }
}
add_action( 'template_redirect', 'my_page_template_redirect' );

Related Posts:

  1. Assigning a role to a specific custom post type (and ignoring other post types)
  2. Hiding posts by other users and non-logged in
  3. Query within a foreach within a query (queryception)
  4. Display custom post front end filter by ACF equals current user
  5. Display only one post each WEEK
  6. List children on child post
  7. Restrict custom post type from appearing with ?post_types=
  8. Show custom post type filtered by category
  9. create parent post using wp_insert_post
  10. Turn on and off custom post type from admin?
  11. Missing Posts in Custom Taxonomy List
  12. Display random posts, but omit the post it is on?
  13. Roles for Custom Post Types
  14. How do I do this with WordPress? Taxonomies?
  15. Disable block with taxonomies at post page
  16. I would like to have different styles for my posts based on the content of each post
  17. How can i add thumbnails images to particular post (using code not admin pannel) in wordpress
  18. Is using taxonomies the way to go here or would custom post types be better?
  19. Showing posts from different categories and from custom post type
  20. Get latest 3 posts from multiple CPT in one query
  21. Allow non-logged in users to see a future post after clicking on a list of future posts
  22. Which post does a taxonomy term belongs to?
  23. List custom taxonomy specific to one custom post type
  24. Post image in WordPress not appearing on home page
  25. How to add a custom-post-type post within another custom-post-type post edit screen using AJAX?
  26. Meta query for custom post type ignored in main query
  27. Changing custom type name hides the posts
  28. Display Ad on Specific Categories
  29. Update Post Meta for a logged in user
  30. Add custom field to Posts and sort by it
  31. Add custom post type settings to wordress default posts
  32. Get related posts of child term of custom post type
  33. Two Custom Post Types Many to Many Relationship
  34. Problem with displaying posts in the CPT category
  35. JS innerhtml changing style when using AJAX
  36. Show titles, date of all posts on single category page
  37. Side effects of Script and Iframe in post
  38. How to change the post type a theme shows by default?
  39. Set a checkmark in a category based on a URL-parameter
  40. Several post types on WP Query by tag and taxonomy
  41. No Permission to add new Page, Post or CPT with Admin role
  42. Update postmeta Parent when post_status child change
  43. custom post type category count shortcode
  44. Portfolio Page for Classic Posts
  45. Add post location with mile radius allowing search
  46. Get the category from custom post type
  47. Is it possible to store Custom Post Type data in separate set of tables and still have wp_post class functionality?
  48. Set up post page like JAMA articles
  49. Users create/join groups
  50. Custom Post Slug same as Parents Category Slug
  51. Shortcode not working with post counter
  52. Disable user from updating certain posts
  53. Changing default ‘posts’ parameters with register_post_type_args
  54. Admin Column does not populate with data
  55. How to integrate a form (Ninja Form or Contact Form 7) with Custom Post Types?
  56. Prioritize posts in query by meta keys?
  57. Homepage’s content is dependent on the custom field values (set automatically), how do I get homepage to update without manually updating page?
  58. Query posts based on the meta key values of logged-in users?
  59. How to restrict author to only access one custom post type ?
  60. Create custom post with custom user rules
  61. Allow users to create posts without logging in?
  62. How to get all tags of a custom post type by id
  63. Associate multiple users to custom post type
  64. How to define which register_post_status goes to which register_post_type?
  65. How to assign permissions for a CPT to a user
  66. Load Next Posts With AJAX not working with custom post type
  67. Limit the post for differents custom post type in the same wp_query
  68. 2 Different Custom Post Types in Submenu
  69. How to assign classes to all elements?
  70. How can I show 1 featured post in a styled element, and the next few below differently styled
  71. Some posts from custom post type to subdomains
  72. How can i create an custom post template for an specific post category? [closed]
  73. How to sort posts in a custom post type by title in ascending order by default?
  74. WP the_posts() on single-cars.php get category link
  75. Conditional for a Single Post That Belongs to a Category?
  76. $post not working working in AJAX plugin with custom post type
  77. Permalink misbehaving in Custom Post Types
  78. Check post on publish for blank title
  79. how to check if custom post title exists or not?
  80. Different Limit number of post on different archive page
  81. How to change “post_class()” for a custom post type?
  82. Modify previous and next post links to current Authors Other posts
  83. Reworking function for counting custom post type posts count
  84. Create new custom post and post category of same name
  85. How can i display movies in profile page, added by an user?
  86. wordpress remove views from action links in a custom post
  87. Load Next WordPress Posts With AJAX?
  88. Help With Warning on creating new Post types?
  89. help
  90. Including link to custom post type in ‘wp_list_pages’ function
  91. Get an array wich contains the post_name of every post that has the custom post_type “pelicula”
  92. Is it a good idea to add a column to the posts table?
  93. wp_insert_post wrong post type [closed]
  94. Multiple pages per post (not pagination)
  95. If there is only one post (show elements) else (show other elements)
  96. Custom User role not working with custom post type
  97. Custom post types and ‘new WP_Query’
  98. Can I change my post type to anything and my site still work?
  99. Variable not working in WP_Query
  100. Display an authors post on a single page only when they are logged in
Categories custom-post-types Tags account, custom-post-types, posts, users
How can I show a custom WordPress menu anywhere I want on my website?
oembed_dataparse filter not running at all for standard YouTube embed

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