最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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!

发布评论

评论列表(0)

  1. 暂无评论