Limit Authors to their Own Posts on front-end excluding admins


I’m trying to limit custom post type “Projects” only to post authors excluding the admins. I’m trying the following code:

function project_view_for_only_author($query) {

  global $current_user;

  if (!current_user_can('manage_options')) {
      $query->set('author', $current_user->ID);
add_action('pre_get_posts', 'project_view_for_only_author');

When i first tried this code, it worked perfectly fine. It limited the CPT to post authors on the backend as well as on the front-end. But later when i created a new user(author), 404 error page not found is appearing instead of the homepage entirely for the every new users created. but the custom post type can be viewed through single-projects.php and strangely the error is there only when i am logged-in from that new user’s(author) account. I tried viewing it from the administrator account, everything seems to be working fine and the custom post type posted by that new user(author) is also showing on the homepage. Not sure whats going wrong over here.

Maybe adding an argument separately for limiting the post display on the front-end would work and eliminate this error. Any help would be appreciated. Thankyou!

, , , Rafiq 8 months 0 Answers 94 views 0

Leave an answer