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?
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
forms needed*
Sorry, autocorrect made some mistakes.
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
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”?).
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 ”). 🙂
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 …
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; ?>
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 🙂
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?
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. 🙂