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

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?

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #2665

    I want to display the results post_author for the logged in 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;
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.