0

Is there a way to restrict search results to only those posted by the logged in user? I have a front-end form the user fills out.

Every user has a front-end dashboard and is able edit their own post. Since they may have many posts I would like them to be able to search for their own posts, but not display posts from other authors (this would only complicate things) Is there a way to do this?

1 Answer 1

2

Something like this should work:

function my_search_filter($query){
  if($query->is_search)
    $query->set('author', get_current_user_id());

  return $query;
};

add_filter('pre_get_posts', 'my_search_filter');
2
  • thank you for the info, not sure how to implement it though. I placed it in the functions.php - but I'm still able to search for other author's posts.
    – user7481
    Commented Sep 7, 2011 at 19:37
  • Doesnt it work as intended?
    – p.a.
    Commented Sep 13, 2012 at 10:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.