0

After submitting the search form, the site directs to "homeurl/post-name/?s=lorem" instead of "homeurl/?s=lorem" when I have permalinks set to "%post-name%" ( not literally 'homeurl', but the domain of the site ).

When I have permalinks set to "/blog/%postname%", the search results url becomes /blog/%postname%/?s=lorem.

The action on the search form (searchform.php) is:

    <?php echo get_home_url(); ?>. 

If I hard code it to the home url that won't work either. It keeps appending the search query after /blog or after the post name.

I even added the following to functions.php, and it had no effect:

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

Any idea what might be going on here and what else I can try? I already deactivated all plugins and tried it. Same story.

***** UPDATE (solution) *****

Because I am a very silly WordPresser, I had

  <?php get_search_form(); ?>

wrapped in form tags

  <form> </form>

I removed the tags and the problem stopped and search is working properly now.

0

The form action should be this like below

action="<?php echo esc_url( home_url( '/' ) ); ?>">
  • Also forgot to tell that remove the function that you added in functions.php to rewrite URL. This should work .. – Md. Amanur Rahman Oct 16 '17 at 18:10
  • Thanks for your response! I have changed the action to what you have suggested. However, I have found the real cause of the issue and it wasn't this. – Jackie Oct 16 '17 at 18:28

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.