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. Allow member to have access to custom post type only. Permission to only edit their own posts
  2. Possible to hide Custom Post Type UI/Menu from specific User Roles?
  3. How to get post content by calling ajax?
  4. Custom post type role permissions won’t let me read
  5. Get the ID of the latest post
  6. Allowing custom role access to custom post type in back end
  7. How to get all children and grandchildren of a hierarchical custom post type?
  8. How do I require authorization / login to view a specific set of posts / pages?
  9. Restrict Contributors to view only their own custom post types?
  10. Why does my custom WP role need edit_posts to edit images?
  11. Custom user role cannot see or modify featured image
  12. Getting the Intersection of Two Custom Taxonomy Terms for a Custom Post Type?
  13. How to assign specific users the capability to edit specific pages / posts / custom post types
  14. Allow user to Edit Posts but not Add New?
  15. Defining capabilities for custom post type
  16. Assign a Custom Role to a Custom Post?
  17. Restrict custom post type view by user role
  18. Creating custom user roles
  19. List most recent image uploads, but only for specific custom post type
  20. Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter
  21. Can I make user role that can only access a certian content type?
  22. Associating an “author” with a custom taxonomy
  23. Get all authors with at least one post of ‘custom post type’
  24. Show the First Post from Each of 3 Different Post Types on a Web Page?
  25. Create user role restricted to specific CPT
  26. How to set individual capabilities on a taxonomy? Or how to re-register an existing taxonomy?
  27. functions.php conditional tag only for custom post type
  28. Retrieving 3 latest post from each of 5 different custom post types
  29. User roles – enable custom posts disable posts
  30. Add Capabilities to Custom Post Type after it has been created [duplicate]
  31. How to not allow custom roles to edit published custom post types?
  32. How to restrict specific post types from being read or added by specific user roles (eg. author)?
  33. How can I get the last posts by user role?
  34. 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?
  35. Custom user role not working as expected
  36. Front End users account with lots of user Roles (not Woocommerce)
  37. Capibilities of CPT WordPress
  38. Error get_posts with Custom Taxonomy and OR relation
  39. How can I remove “Add new” button on custom post type
  40. Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
  41. Get post from Category by Priority
  42. 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
  43. How can you use a page for a custom post type?
  44. Custom user roles
  45. Editor role isn’t seeing specific post type posts with only post_type parameter
  46. Running get_posts within get_posts to get children of children
  47. WordPress get all post with like in terms [duplicate]
  48. Certain number of posts with certain excerpt length
  49. Decrement term in for each
  50. Help ordering custom query by Title, Ascending
  51. Allow viewing the edit screen for a post type, but not make/save/publish/update changes
  52. This wp_query will not return any posts and only seems to work with post_status inherit?
  53. Weird capabilities / roles behavior
  54. Control what custom posts a user can see
  55. Custom Post Type only display items created by user
  56. Add role privileges of the custom post type
  57. Popup panel is only displaying 1 entry ignoring all others
  58. Prevent author role from editing others posts
  59. Fetch posts list from fist CPT taxonomy term and list under 2. CPT
  60. creating different edit screens for different roles
  61. Default taxonomy ‘post_tag’ added to CPT managing by custom role : nothing in the metabox
  62. Custom taxonomy archive is visible to only subscibed users
  63. CPT, Custom User Role and problem with user manager
  64. one get_posts to return a number of custom posts for each meta value
  65. How to create content automatically when a post is published?
  66. Custom post type media upload error with custom user role
  67. Query posts by a type and another type only if post is in specific category
  68. How can I give WordPress child custom post type the same title and slug as its parent?
  69. Display and Allow users to edit their own profiles
  70. Custom Post Type supports author
  71. Custom Post Type children and grand-children in one list
  72. shortcode for recent custom type post
  73. Capability to read user’s own draft post of CPT
  74. Custom role, capabilities, and post type: preview button wrecks things
  75. Reservations as Post
  76. Custom post type & role issues
  77. How do I display the index position of a post from a custom post type?
  78. Custom role can’t create permalink
  79. empty dashboard for custom role
  80. Read-Only custom post type
  81. How do I correctly query posts from a post ID?
  82. Custom Post Type meta value is being saved in array, instead of just the string (as value)
  83. Custom Plugin w/ Custom Post Types – Custom Posts Are Showing at all Privilege Levels – Is this possible to adjust?
  84. Display posts from another page on home page in wp-editor
  85. show custom post’s post in two different divs [duplicate]
  86. WordPress Roles
  87. Add Category names to post lists of custom post type
  88. How to I add count of custom posts listed in a post as a prefix to its title
  89. How to get permalink of post based on post name?
  90. Create a hierarchical list of posts that’s grouped and nested by category
  91. How can I show a field ID in Elementor with Request Parameter
  92. post__in select all custom posts and not the selected array of ids
  93. Need some suggestions with Relationship fields and CPT/ACF
  94. get_posts() return NULL using a custom plugin and add_submenu_page() with Post Type
  95. WordPress duplicating posts from single loop
  96. get_posts() function does not honor correct post type
  97. Frontend delete CPT post with custom role user
  98. WordPress User Role Permissions For Custom Post Type
  99. Allowing a CPT post to be edited by a single user role
  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
  • Post Navigation Elementor
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • WPFacet multiple loop displaying duplicate content
  • Get the name of the template/*html file used
  • Fix: WordPress.DB.PreparedSQL.NotPrepared Plugin Check (PCP)
  • Removing unnecessary CSS and JS code from wp_head()
  • hexagonal image gallery. Am I on the right track? [closed]
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
© 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