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:
- Allow member to have access to custom post type only. Permission to only edit their own posts
- Possible to hide Custom Post Type UI/Menu from specific User Roles?
- How to get post content by calling ajax?
- Custom post type role permissions won’t let me read
- Get the ID of the latest post
- Allowing custom role access to custom post type in back end
- How to get all children and grandchildren of a hierarchical custom post type?
- How do I require authorization / login to view a specific set of posts / pages?
- Restrict Contributors to view only their own custom post types?
- Why does my custom WP role need edit_posts to edit images?
- Custom user role cannot see or modify featured image
- Getting the Intersection of Two Custom Taxonomy Terms for a Custom Post Type?
- How to assign specific users the capability to edit specific pages / posts / custom post types
- Allow user to Edit Posts but not Add New?
- Defining capabilities for custom post type
- Assign a Custom Role to a Custom Post?
- Restrict custom post type view by user role
- Creating custom user roles
- List most recent image uploads, but only for specific custom post type
- Confusion with adding meta capabilities to a role after registering a Custom Post Type with corresponding ‘capability_type’ parameter
- Can I make user role that can only access a certian content type?
- Associating an “author” with a custom taxonomy
- Get all authors with at least one post of ‘custom post type’
- Show the First Post from Each of 3 Different Post Types on a Web Page?
- Create user role restricted to specific CPT
- How to set individual capabilities on a taxonomy? Or how to re-register an existing taxonomy?
- functions.php conditional tag only for custom post type
- Retrieving 3 latest post from each of 5 different custom post types
- User roles – enable custom posts disable posts
- Add Capabilities to Custom Post Type after it has been created [duplicate]
- How to not allow custom roles to edit published custom post types?
- How to restrict specific post types from being read or added by specific user roles (eg. author)?
- How can I get the last posts by user role?
- 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?
- Custom user role not working as expected
- Front End users account with lots of user Roles (not Woocommerce)
- Capibilities of CPT WordPress
- Error get_posts with Custom Taxonomy and OR relation
- How can I remove “Add new” button on custom post type
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- Get post from Category by Priority
- 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
- How can you use a page for a custom post type?
- Custom user roles
- Editor role isn’t seeing specific post type posts with only post_type parameter
- Running get_posts within get_posts to get children of children
- WordPress get all post with like in terms [duplicate]
- Certain number of posts with certain excerpt length
- Decrement term in for each
- Help ordering custom query by Title, Ascending
- Allow viewing the edit screen for a post type, but not make/save/publish/update changes
- This wp_query will not return any posts and only seems to work with post_status inherit?
- Weird capabilities / roles behavior
- Control what custom posts a user can see
- Custom Post Type only display items created by user
- Add role privileges of the custom post type
- Popup panel is only displaying 1 entry ignoring all others
- Prevent author role from editing others posts
- Fetch posts list from fist CPT taxonomy term and list under 2. CPT
- creating different edit screens for different roles
- Default taxonomy ‘post_tag’ added to CPT managing by custom role : nothing in the metabox
- Custom taxonomy archive is visible to only subscibed users
- CPT, Custom User Role and problem with user manager
- one get_posts to return a number of custom posts for each meta value
- How to create content automatically when a post is published?
- Custom post type media upload error with custom user role
- Query posts by a type and another type only if post is in specific category
- How can I give WordPress child custom post type the same title and slug as its parent?
- Display and Allow users to edit their own profiles
- Custom Post Type supports author
- Custom Post Type children and grand-children in one list
- shortcode for recent custom type post
- Capability to read user’s own draft post of CPT
- Custom role, capabilities, and post type: preview button wrecks things
- Reservations as Post
- Custom post type & role issues
- How do I display the index position of a post from a custom post type?
- Custom role can’t create permalink
- empty dashboard for custom role
- Read-Only custom post type
- How do I correctly query posts from a post ID?
- Custom Post Type meta value is being saved in array, instead of just the string (as value)
- Custom Plugin w/ Custom Post Types – Custom Posts Are Showing at all Privilege Levels – Is this possible to adjust?
- Display posts from another page on home page in wp-editor
- show custom post’s post in two different divs [duplicate]
- WordPress Roles
- Add Category names to post lists of custom post type
- How to I add count of custom posts listed in a post as a prefix to its title
- How to get permalink of post based on post name?
- Create a hierarchical list of posts that’s grouped and nested by category
- How can I show a field ID in Elementor with Request Parameter
- post__in select all custom posts and not the selected array of ids
- Need some suggestions with Relationship fields and CPT/ACF
- get_posts() return NULL using a custom plugin and add_submenu_page() with Post Type
- WordPress duplicating posts from single loop
- get_posts() function does not honor correct post type
- Frontend delete CPT post with custom role user
- WordPress User Role Permissions For Custom Post Type
- Allowing a CPT post to be edited by a single user role
- remove pagination from a single blog entry