I want to searchable archives like following:

Default Search:

/search/{keyword} -> search.php -> get_template_part('archive-default-search');

Default Custom Archive:

/support -> archive-support.php


/support/search/{keyword} -> search.php -> get_template_part('archive-support-search');

I could not achieve this. How can I do it?

add_action( 'init',  function() {
    add_rewrite_rule( '/(.+)/search/(.+)/?$', 'index.php?post_type=$matches[1]&s=$matches[2]', 'top' );
} );
  • Is it necessary to match the post type? Given that a post type archive permalink can be different from its internal name, this won't work for a lot of post types. It would be easier to hardcode. Also, did you flush/resave the permalinks? Also you will need a separate rewrite rule for /search. I also recommend instlaling monkeyman rewrite rule analyser so that you can see what rules are loaded, what rules match a URL, and what the variables and priority will be, it takes a lot of the guessing out of things
    – Tom J Nowell
    Feb 2, 2021 at 11:27

1 Answer 1

add_action( 'init',  function() {
    add_rewrite_rule( '(.+)/search/(.+)/?$', 'index.php?post_type=$matches[1]&s=$matches[2]', 'top' );
} );

It works done.

  • What did you change to make it work? 🤔
    – Tom J Nowell
    Feb 2, 2021 at 12:19
  • add_rewrite_rule( '/...') to ('...'), thanks for your time.
    – km onur
    Feb 2, 2021 at 13:48

Your Answer

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

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