This website uses cookies to personalize your experience. By using this website you agree to our cookie policy.

Reply To: How to search only in posts where the author is the user?

Home Forums Product Support Forums Ajax Search Pro for WordPress Support How to search only in posts where the author is the user? Reply To: How to search only in posts where the author is the user?

#2667
Ernest MarcinkoErnest Marcinko
Keymaster

Hi again!

If I understand correctly, then you only want to show results, that were created by the current logged in users?
This is only possible by modifying the search source or creating a filtering function. Try to add this code anywhere to your theme functions.php file:

add_filter( 'asp_pagepost_results', 'asp_filter_the_post_author', 1, 1 );
 
function asp_filter_the_post_author( $pageposts ) {
  foreach ($pageposts as $k=>$v) {
    global $current_user;
    get_currentuserinfo();

    $user = get_user_by( 'id', $current_user->ID );
 
    // Get the user nicename 
    $author = $user->data->user_nicename;              
 
    // Remove the post from the array if the author is not the logged in user
    if ($pageposts[$k]->author != $author)
      unset($pageposts[$k]);
  }
 
  return $pageposts;
}