0

Have a search page /search/ where we currently use URLs formatted like /search/?q=query for showing search results.

Now we would like to show the search results with a URL like this /search/query/

But for some reason WordPress doesn't like that. Whenever we try to go to the search results URL, we are redirected to the best match for "query".

Example /search/barista-coffee/ => we are redirected to the post /barista-coffee-experiences/ instead of staying on /search/barista-coffee/.

Anyone know how to fix this, so we can use search results urls like /search/query/ instead of /search/?q=query?

  • 1
    Is the sole purpose of the search page and page template so that you can put the search at /search/? Are you aware of the s parameter and the search.php template? – Tom J Nowell Sep 13 '16 at 22:33
  • Am aware of the s parameter, but this is data search based on custom tables. The data is not in WP (so it's not custom posts etc.) – Louisa Sep 14 '16 at 5:00
0

It's better to share some of your code that already you used.

And you mention first line that you have search page ? what exactly you mean by that ?

WordPress have default search page did you mention that one ? if you are then try this code

function change_search_url_rewrite() {
    if ( is_search() && ! empty( $_GET['s'] ) ) {
        wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
        exit();
    }   
}
add_action( 'template_redirect', 'change_search_url_rewrite' );

But if you have custom page with search slug then you have to do this little-bit different way. However built-in search use always good idea. Still you want to display search result after page slug like domain.com/page/search-term then first you have to make search form and pass form url with your specific page one. like <form role="search" method="get" class="search-form" action="<?php echo home_url( '/page-slug/' ); ?>"> and then use this code for finishing.

add_action( 'template_redirect', 'change_search_url_rewrite' );
function change_search_url_rewrite() {
    if (is_search()) {
        $search_url = get_bloginfo('url') .'/search/'. urlencode(get_query_var('s')) . ((get_query_var('paged')) ? '/page/'. get_query_var('paged') .'/' : '/');
        if (!empty($_GET['s']) || !empty($_GET['paged']))
            wp_redirect($search_url);
    }
}

hope it makes sense!

  • Thanks. The search page is a standard page. In functions.php we have made filters like this: if(strpos(get_permalink(), "/soeg/") !== false) to identify that we are on a search page. – Louisa Sep 14 '16 at 5:03
  • Not sure what you mean by "have to make search form"? – Louisa Sep 14 '16 at 5:03
  • Means create a custom search form by your self and pass your page url in form action. like <form role="search" method="get" class="search-form" action="<?php echo home_url( '/page-slug/' ); ?>"> – Mohammad Limon Sep 14 '16 at 9:17

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.