0

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

Mixed:

/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' );
} );
1
  • 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 at 11:27
0
add_action( 'init',  function() {
    add_rewrite_rule( '(.+)/search/(.+)/?$', 'index.php?post_type=$matches[1]&s=$matches[2]', 'top' );
} );

It works done.

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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