1

I want to add /category/ to a author archive page to filter just the posts from a specific category from all the authors, something like this: mysite.com/author/me/movies

Is there a way to do it using a function? I know how to do using something like this mysite.com/author/me/?category=movies, but I want using correct permalinks.

1 Answer 1

0

You can achieve this by adding a new rewrite rule using 'add_rewrite_rule`.

If the category is the default WordPress category, you won't have to register a query_var but if your category is a custom category, then you would have to register a query_var using the query_vars filter.

If category is the default WordPress category:

function wpse256215_custom_rewrite_rule() {
    add_rewrite_rule('^author/([^/]*)/([^/]*)/?','index.php?author_name=$matches[1]&category_name=$matches[2]','top');
}
add_action('init', 'wpse256215_custom_rewrite_rule', 10, 0);

If category is a custom query variable (author_category):

function wpse256215_custom_rewrite_rule() {
    add_rewrite_rule('^author/([^/]*)/([^/]*)/?','index.php?author_name=$matches[1]&author_category=$matches[2]','top');
}
add_action('init', 'wpse256215_custom_rewrite_rule', 10, 0);

function wpse256215_query_vars( $query_vars ) {
    $query_vars[] = 'author_category';
    return $query_vars;
}
add_filter( 'query_vars', 'wpse256215_query_vars' );

To use the custom query variable in your template, you can then use the get_query_var function:

$author_category = get_query_var( 'author_category' );

REFERENCE:

9
  • Works! Thanks @Tunji. Btw, the page title dont assume the author name or anything. Can I add something to this rewrite rule to configure how the page titles will work in this cases? Feb 13, 2017 at 10:33
  • You should either edit your archive.php template file or use the_title filter
    – Tunji
    Feb 13, 2017 at 11:04
  • I had problem with pagination, and i did this: function wp_author_plus_category () { add_rewrite_rule('^author/([^/]*)/([^/]*)/?','index.php?author_name=$matches[1]&category_name=$matches[2]','top'); add_rewrite_rule('^author/([^/]*)/([^/]*)/page/([0-9]{1,})/?','index.php?author_name=$matches[1]&category_name=$matches[2]&paged=$matches[4]','top'); } add_action('init', 'wp_author_plus_category', 10, 0); but don't work. What i'm doing wrong? Feb 13, 2017 at 11:07
  • Can you paste a sample url with pagination? Plus your regex looks wrong
    – Tunji
    Feb 13, 2017 at 11:38
  • http://cinemacomrapadura.com.br/author/siqueira/criticas/?theme=chazelle the ?theme is for forcing the theme that I'm developing right now. Feb 13, 2017 at 12:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.