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?
- 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?
- Why does my custom WP role need edit_posts to edit images?
- 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
- Defining capabilities for custom post type
- Assign a Custom Role to a Custom Post?
- Creating custom user roles
- Can I make user role that can only access a certian content type?
- How to set individual capabilities on a taxonomy? Or how to re-register an existing taxonomy?
- 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 restrict specific post types from being read or added by specific user roles (eg. author)?
- Custom user role not working as expected
- How to redirect to a passworded page based on password entered on login screen
- Which User Role for Custom Post Type Menu – wp_nav
- post__in not taking my list of IDs
- Getting a custom post type image using ‘get_post_custom’
- How to allow “Add New” capability of CPT when links to its UI are placed as a submenu?
- Custom Role can’t trash Custom Post Type
- Limit access to page depending on user level
- Why does current_user_can(‘edit_posts’) return true, but current_user_can(‘edit_post’, $post->ID) returns false?
- How to publish custom post type for custom role user and not “submit for review”?
- Registration and Custom Post Types – How to synchronize information?
- Limit users by custom taxonomy and user roles
- Allow Administrator role access to custom capabilities [duplicate]
- How to enable a custom post type to custom user role in WordPress
- Restrict user to only one custom post type
- How to restrict CPT post’s fronted view only for specific user roles?
- current_user_can() return FALSE but debugging says TRUE
- map_meta_cap woes
- Roles for Custom Post Types
- Role scoping for contributors down to the field level
- Custom post types as sub menu pages and role capabilities issue
- Hiding posts by other users and non-logged in
- WordPress multiple custom post types capability conflict in a single menu
- How to set individual capability of post type in individual category
- I want to create custom post in which user can share files like PDF & DOCx
- Stuck in Order by more then one
- WooCommerce Customer Role Delete Custom Post Type
- Allow Contributor to edit but not update a published post
- Disable user from updating certain posts
- Access to CPT but not to ‘post’ post type
- Deny user access to edit post while allow him to edit custom post type
- ‘exclude’ argument is not working with get_posts for a custom post type
- How to get the intersection of two terms both from different custom taxonomies
- How to show featured image block in custom post type for Author?
- Reworking function for counting custom post type posts count
- Get only used meta_values
- Recoverable Fatal Error – Object of class WP_Post could not be converted to string
- WordPress custom post type capabilities issue
- Custom User role not working with custom post type
- How to Make infinite loop of post
- get_posts seems to be skipping the last Post
- Building Forums with Custom Post Types
- Displaying multiple post types on home page
- Conflict between wp_list_pages and get_posts – list pages not displaying
- meta query not retrieving posts
- Enable comments for post when comments meta box removed
- Display a random customposttype2 excerpt in single-custompostype1.php that shares same taxonomy
- date_query empty results with custom post type
- get_posts() returns empty on custom post type /wp-admin/edit.php
- Why “Mine” is the default view when adding ‘capability_type’ in register_post_type
- Added new role with custom capability. But cannot access the page
- Create relationship between custom post types and users
- Restrict admin pages for specific user role
- Conditional editing CPT – using editor’s role and author’s usermeta
- How to only display all posts to a custom User Role?
- get_posts wont produce a list of custom type from a given category [duplicate]
- Remove plugin settings from post creation page for a user role
- Allowing custom role user to edit post assigned to them but don’t let them create new custom type post
- I want to show post details based on roles user login
- Multiple useres editing specified content
- Instructor can view, edit, delete only theirs courses
- week days sorting based on starting day
- Capibilities of CPT WordPress
- Why get_posts() returns empty array while I am trying to get posts from some specific taxonomies and work properly with others?
- Editor role isn’t seeing specific post type posts with only post_type parameter
- Help ordering custom query by Title, Ascending
- This wp_query will not return any posts and only seems to work with post_status inherit?
- Control what custom posts a user can see
- Prevent author role from editing others posts
- Fetch posts list from fist CPT taxonomy term and list under 2. CPT
- Default taxonomy ‘post_tag’ added to CPT managing by custom role : nothing in the metabox
- Custom taxonomy archive is visible to only subscibed users
- How to create content automatically when a post is published?
- Custom role, capabilities, and post type: preview button wrecks things
- How do I display the index position of a post from a custom post type?
- 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?
- show custom post’s post in two different divs [duplicate]
- How to I add count of custom posts listed in a post as a prefix to its title
- 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