• 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.

Viewing 11 replies - 1 through 11 (of 11 total)
  • Moderator keesiemeijer

    (@keesiemeijer)

    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?

    Thread Starter multiformeingegno

    (@lorenzone92)

    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

    Thread Starter multiformeingegno

    (@lorenzone92)

    forms needed*
    Sorry, autocorrect made some mistakes.

    Moderator keesiemeijer

    (@keesiemeijer)

    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

    Thread Starter multiformeingegno

    (@lorenzone92)

    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”?).

    Thread Starter multiformeingegno

    (@lorenzone92)

    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 ”). 🙂

    Thread Starter multiformeingegno

    (@lorenzone92)

    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 …

    Moderator keesiemeijer

    (@keesiemeijer)

    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; ?>

    Thread Starter multiformeingegno

    (@lorenzone92)

    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 🙂

    Moderator keesiemeijer

    (@keesiemeijer)

    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?

    Thread Starter multiformeingegno

    (@lorenzone92)

    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. 🙂

Viewing 11 replies - 1 through 11 (of 11 total)
  • The topic ‘Display author posts using search parameter (not author template/query_posts)’ is closed to new replies.