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

Display content based on a users login

You could try this, IF the current user is logged in AND not Adminstrator it will pull all posts from the current user, otherwise it should default and show all posts from all users.

<?php 
    function filter_posts_by_author( $query ) {
        if( is_user_logged_in() ) {
            if( !current_user_can( 'administrator' ) ) {
                global $current_user; 
                get_currentuserinfo();
                $query->set( 'author', $current_user->ID );
            }
        }
        else {
            wp_die('Sorry, you do not have access to this page. Please <a href="/#/">sign in</a> to view this page.');
        }
    }
    add_action( 'pre_get_posts', 'filter_posts_by_author' );
?>

Related Posts:

  1. Custom post type role permissions won’t let me read
  2. Get the ID of the latest post
  3. Custom user role cannot see or modify featured image
  4. Allow user to Edit Posts but not Add New?
  5. Restrict custom post type view by user role
  6. List most recent image uploads, but only for specific custom post type
  7. Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter
  8. Associating an “author” with a custom taxonomy
  9. Show the First Post from Each of 3 Different Post Types on a Web Page?
  10. functions.php conditional tag only for custom post type
  11. How can I get the last posts by user role?
  12. custom post type upcoming post and past post
  13. Adding capabilities to default roles
  14. How to get only one category of custom post type?
  15. How to query custom post type by term?
  16. Get random custom posts from a custom post type
  17. grant a privilege for roles (for example editors) and higher
  18. Quickest way to get last or oldest post date – WP Query
  19. Role Capabilities: Add New Ones?
  20. Prevent authors from creating new posts of specific custom-post-type
  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. using add_role function to make some users to create a selected type post
  25. Filter the content from a post globally immediately after is fetched from the database
  26. Combine query_posts() and get_posts() into single query
  27. Why is my Custom Post Type not showing up after adding capabilities?
  28. Are there sub-systems in WordPress that would employ data structures beyond CPT that a `get_posts` won’t be able to catch?
  29. Shortcode for display posts on home page show only one post
  30. WordPress count child posts of each parent custom post type and use in json array for google maps jquery plugin
  31. only a certain role has access to a cpt
  32. Display all post titles of current post type
  33. Define new user capability for custom post types?
  34. Users create/join groups
  35. Edit the author of custom post type
  36. Allow add new post access to custom post but not wp post for some role
  37. Create custom post with custom user rules
  38. Display latest x posts from all categories in Custom Post Type/Taxonomy
  39. Want to be able to sign up subscribers as authors
  40. Get only used meta_values
  41. Custom query for custom post_type
  42. disable quickedit for specific custom post type user role
  43. how to associate several authors to a custom post type
  44. populate array with posts
  45. Displaying multiple post types on home page
  46. Conflict between wp_list_pages and get_posts – list pages not displaying
  47. WordPress: Custom User Role cannot access Custom Post Type | “Sorry, you are not allowed to access this page”
  48. Select other roles as custom post authors
  49. Is it possible to use array_walk() to append terms to an array of posts?
  50. custom hierarchical taxonomy and custom post type list contains surplus posts
  51. Preventing role reading others posts
  52. Can’t pull posts of a Custom Post Type based on the custom taxonomy of the CPT
  53. 4 posts per page from single category
  54. Enable comments for post when comments meta box removed
  55. Roles and Capabilities in Custom Post Types
  56. Query custom posts of logged in user only
  57. Display a random customposttype2 excerpt in single-custompostype1.php that shares same taxonomy
  58. How to query custom post types with multiple keys?
  59. Change user role when user’s first post gets published
  60. Orderby meta_key/meta_value not displaying anything, am I overlooking anything?
  61. Clone wp-admin/users.php (Users Admin Page)
  62. Get_post_custom not fetching value from array wordpress
  63. Added new role with custom capability. But cannot access the page
  64. I’m having trouble with Related Product “tax_query” ‘terms’ value
  65. Conditional editing CPT – using editor’s role and author’s usermeta
  66. Remove plugin settings from post creation page for a user role
  67. Get all posts by many custom post types
  68. Modify default Related Posts Code to Custom Post Type
  69. Allowing custom role user to edit post assigned to them but don’t let them create new custom type post
  70. Front-end submitted post is published with admin ID as author
  71. Query a custom taxonomy in a function to create an csv file
  72. WooThemes – Vendors / Bookings – Allow Vendors to manage resources
  73. week days sorting based on starting day
  74. Front End users account with lots of user Roles (not Woocommerce)
  75. Get post from Category by Priority
  76. How can you use a page for a custom post type?
  77. Running get_posts within get_posts to get children of children
  78. WordPress get all post with like in terms [duplicate]
  79. Decrement term in for each
  80. Allow viewing the edit screen for a post type, but not make/save/publish/update changes
  81. Custom Post Type only display items created by user
  82. Add role privileges of the custom post type
  83. creating different edit screens for different roles
  84. CPT, Custom User Role and problem with user manager
  85. one get_posts to return a number of custom posts for each meta value
  86. How can I give WordPress child custom post type the same title and slug as its parent?
  87. Display and Allow users to edit their own profiles
  88. Custom Post Type supports author
  89. Custom Post Type children and grand-children in one list
  90. Capability to read user’s own draft post of CPT
  91. Custom post type & role issues
  92. Custom role can’t create permalink
  93. Read-Only custom post type
  94. How do I correctly query posts from a post ID?
  95. Custom Plugin w/ Custom Post Types – Custom Posts Are Showing at all Privilege Levels – Is this possible to adjust?
  96. WordPress Roles
  97. How to I add count of custom posts listed in a post as a prefix to its title
  98. How to get permalink of post based on post name?
  99. WordPress User Role Permissions For Custom Post Type
  100. remove pagination from a single blog entry
Categories custom-post-types Tags custom-post-types, get-posts, login, user-roles
Show Post Content with AJAX
How do I replace “Username” in the WordPress login form?

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