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 Answers
2

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. 🙂

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *