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?

This topic contains 1 reply, has 2 voices, and was last updated by Ernest Marcinko Ernest Marcinko 7 years, 8 months ago.

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 Marcinko
    Ernest 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;
    }
    
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.