WordPress.org

Ready to get started?Download WordPress

Forums

Display author posts using search parameter (not author template/query_posts) (12 posts)

  1. MultiformeIngegno
    Member
    Posted 1 year ago #

    Is there a way to retrieve a list of all the posts by an author using search (example: ?s&author=xxx)? I can't use query_posts or author template (don't ask why, take it as a condition).
    And it should be a public query (the filter in admin panel isn't good).

    Thanks in advance.

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    So you want to use a form where users can choose from a list of authors and after submitting show only posts from the chosen author on the search results page?

  3. MultiformeIngegno
    Member
    Posted 1 year ago #

    No, I'd like to be able to use authors as a search parameters. No firms needed (just the ability to use ?s&author=username or something like that

  4. MultiformeIngegno
    Member
    Posted 1 year ago #

    forms needed*
    Sorry, autocorrect made some mistakes.

  5. keesiemeijer
    moderator
    Posted 1 year ago #

    No forms needed

    How would users choose the author?

    I think this in the url works as you want it to:
    /?s=hello+world&author_name=admin

  6. MultiformeIngegno
    Member
    Posted 1 year ago #

    I don't need users to choose the author. *I* need the filter, don't worry about practical use :)
    Anyway, the URL structure works fine, the only thing left is to have no search terms, I want all posts from an author to be displayed (search term should be blank, how can I set it to "none"?).

  7. MultiformeIngegno
    Member
    Posted 1 year ago #

    Something like /?s=%20&author_name=username works, but if there's something that sets search term to none would be better (to not display "search results for ''). :)

  8. MultiformeIngegno
    Member
    Posted 1 year ago #

    For this to be perfect I'm looking also for a way to set the search results to start from Xth entry, for example startfrom=6 ...

  9. keesiemeijer
    moderator
    Posted 1 year ago #

    Is there any reason you want to use the search template?

    with this in your theme's functions.php you can show the author posts on a search template file:

    add_action( 'pre_get_posts', 'alter_search_query' );
    function alter_search_query( $query ) {
      // not an admin page and is the main query
      if (!is_admin() && $query->is_main_query()){
        if(is_search() && ($query->query_vars['s'] == 'none')){
    
        	$author = get_query_var('author_name');
        	if($author != '') {
          	$query->set('author_name', $author);
          	// set search query 'none' to empty string ''
          	$query->set('s', '');
          	$query->set('post_type', 'post');
          }
        }
      }
    }

    the url has to be like this: /?s=none&author_name=admin

    On the search template you can use a conditional to check if the search query is empty:

    <?php if(get_search_query() ==  '') : ?>
    <!-- do stuff for empty search query -->
    <?php else : ?>
    <!-- do stuff for not empty search query -->
    <?php endif; ?>
  10. MultiformeIngegno
    Member
    Posted 1 year ago #

    Thanks for the example. Anyway I have troubles understanding what your code does exactly. What are the differences between that and the method with parameters in URL without code in functions.php?

    Thanks again :)

  11. keesiemeijer
    moderator
    Posted 1 year ago #

    Ah yes, not really needed now, but if you want to use a offset later you can use it to alter the query. Why the need to use a search template file? Is it's for the layout on that page?

  12. MultiformeIngegno
    Member
    Posted 1 year ago #

    Here's the page http://goo.gl/QC0P5.
    As you can see the latest 9 articles from an author. At the end a link to ALL his articles. :)

Topic Closed

This topic has been closed to new replies.

About this Topic