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