How to change search query string to permalink?
-
I have redirected search page to new pretty URL, but that page now shows error 404. What extra code do I have to add to deal with URL on that error page.
Another question is, how to check if re-writing is done and
custom_rewrite_rule()(see below) is functioning properly.Here is my code so far:
add_action( 'template_redirect', 'change_search_url_rewrite' ); function change_search_url_rewrite() { if( isset( $_GET['search_lat'] ) || isset( $_GET['search_lng'] ) || isset( $_GET['search_city'] ) ) { $new_uri = array( ); if( isset( $_GET['search_lat'] ) && strlen( $_GET['search_lat'] ) ) { $new_uri[] = urlencode( 'lat-'.$_GET['search_lat'] ); } if( isset( $_GET['search_lng'] ) && strlen( $_GET['search_lng'] ) ) { $new_uri[] = urlencode( 'lng-'.$_GET['search_lng'] ); } if( isset( $_GET['sort'] ) && strlen( $_GET['sort'] ) ) { $new_uri[] = urlencode( 'sort-'.$_GET['sort'] ); } if( isset( $_GET['search_city'] ) && strlen( $_GET['search_city'] ) ) { $new_uri[] = urlencode( 'city-'.$_GET['search_city'] ); } if( isset( $_GET['search_category'] ) && strlen( $_GET['search_category'] ) ) { $new_uri[] = urlencode( 'category-'.$_GET['search_category'] ); } if( isset( $_GET['search_type'] ) && strlen( $_GET['search_type'] ) ) { $new_uri[] = urlencode( 'type-'.$_GET['search_type'] ); } if( isset( $_GET['search_neighborhood'] ) && strlen( $_GET['search_neighborhood'] ) ) { $new_uri[] = urlencode( 'neighborhood-'.$_GET['search_neighborhood'] ); } wp_redirect( home_url( "/properties-search-results-2/" ) . implode( '/' , $new_uri ) ); exit(); } } add_action('init', 'custom_rewrite_rule', 10, 0); function custom_rewrite_rule() { add_rewrite_rule( "properties-search-results-2/category-([^/]*)/city-([^/]*)/lat-([^/]*)/lng-([^/]*)/neighborhood-([^/]*)/sort-([^/]*)/type-([^/]*)/?" , '?page_id=16&search_lat=$matches[3]&search_lng=$matches[4]&sort=$matches[6]&search_city=$matches[2]&search_category=$matches[1]&search_type=$matches[7]&search_neighborhood=$matches[5]' , 'top' ); }The page at
/properties-search-results-2/has page id of 16, so I hard coded it to rewrite function. The search was working on this page before rewriting.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
The topic ‘How to change search query string to permalink?’ is closed to new replies.