3

Sorry for the basic question, every Google query I try yields no useful results.

How do I change the search results directory in Wordpress? Currently all searches need to be on the root to produce results. i.e.

www.domain.tld/?s=query

But I'd like to be able to have two searches (which I currently already have working independently):

  • www.domain.tld/blog/?s=query
  • www.domain.tld/somethingelse/?s=query

I've tried adding the directories to my search form actions, and although I've got Wordpress loading the right template for each search, the searches never produce any results. They only produce results when searching on the root.


So it turns out that coming back to this I was able to just include the directory in the search form action. The strange this is that I'd experimented with this at quite some length - and gotten nowhere. So I think that something else might have been at play preventing this from working for some reason. Annoying mystery!

2
  • You mean you wants some sort of custom search? or just change url look?
    – Amirmasoud
    Commented Jul 2, 2015 at 18:29
  • I mean I want the query to execute on a specific path. Commented Jul 2, 2015 at 23:55

2 Answers 2

6

You can create a new page. Let's say you want to have: http://example.com/mysearch/

Create a page that will have that URL structure. Next, Search form -> Go to and on you search form action do:

<form role="filter-search" method="get" id="sd_searchform_filter" action="<?php 
    echo home_url( '/mysearch/' ); ?>">...

Now go to functions.php (or where you want this function) and now we doing tricks!

function isu_search_url( $query ) {

    $page_id = 12; // This is ID of page with your structure -> http://example.com/mysearch/
    $per_page = 10;
    $post_type = 'activity'; // I just modify a bit this querry

    // Now we must edit only query on this one page
    if ( !is_admin() && $query->is_main_query() && $query->queried_object->ID == $page_id  ) {
        // I like to have additional class if it is special Query like for activity as you can see
        add_filter( 'body_class', function( $classes ) {
            $classes[] = 'filter-search';
            return $classes;
        } );
        $query->set( 'pagename', '' ); // we reset this one to empty!
            $query->set( 'posts_per_page', $per_page ); // set post per page or dont ... :)
            $query->set( 'post_type', $post_type ); // we set post type if we need (I need in this case)
            // 3 important steps (make sure to do it, and you not on archive page, 
            // or just fails if it is archive, use e.g. Query monitor plugin )
            $query->is_search = true; // We making WP think it is Search page 
            $query->is_page = false; // disable unnecessary WP condition
            $query->is_singular = false; // disable unnecessary WP condition
        }
}
add_action( 'pre_get_posts', 'isu_search_url' );

Now it works, you don't have to change .htaccess, etc. Pagination will work properly. :)

3
  • 1
    This was VERY helpful! :)
    – Greg
    Commented Sep 11, 2017 at 19:03
  • This works great! I'm noticing that on the homepage it's throwing a — "Trying to get property 'ID' of non-object" error on the homepage. Any ideas?
    – Gcamara14
    Commented Aug 12, 2020 at 19:00
  • I think $query->queried_object->ID might be a problem, just try add condition also to check if $query->queried_object exist.
    – Isu
    Commented Sep 1, 2020 at 8:31
1

You can do this with functions.php file:

function fb_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', 'fb_change_search_url_rewrite' );

also possible via htacces rules:

# search redirect
# this will take anything in the query string, minus any extraneous values, and turn them into a clean working url
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ /search/%1/? [NC,R,L]

source

4
  • Thanks Amir, I'll take a look at the first option later today. I can't do a rewrite as I'm actually using the site index for a truly static html page (very specific and unusual setup) which is why I can't have the search execute there, so merely rewriting it won't work unfortunately. Commented Jul 3, 2015 at 10:45
  • Hi Amir, I haven't gotten round to implementing this suggestion yet - but just looking at it - isn't this just redirecting the query to a place where the search won't work? i.e. how is this any different to just using /search/ as part of the form action? Commented Jul 3, 2015 at 13:16
  • Ok so this is weird - before I even attempted to implement this solution I had a quick fiddle with the action again. For some reason running the search on a different sub directory is now working. This is particularly strange as I'd experimented with this already with no luck, even trying to re-save the permalink settings to force things into action. Is there something obvious that may have stopped and started this from working?? Commented Jul 3, 2015 at 13:31
  • This doesn't work for me. Getting a 404 with the PHP version of this.
    – ggedde
    Commented Oct 14, 2019 at 20:28

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.