by default wordpress search results go to sitename.com/?s=terms but i would like it be changes to sitename.com/search/?s=terms
I have tried using the
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' );
This get close as now when you do search it goes to search/searchterm but does not have the ?s= in the url
Alternatively, you can change the rule in .htaccess
Add the following on the line after "RewriteBase /":
RewriteRule ^?s=(.*)$ search/?s=$1
See reference here: http://wordpress.org/support/topic/change-default-search-string-s-to-permalink-structure
Related
I'm a wordpress newbie. I will try to describe my problem in the clearest way possible.
I'm trying to do two things here:
Remove CPT from a permalink.
Add a custom taxonomy type where it used to be the CPT in the permalink.
Permalinks on site used to be like this:
http://example.com/custom-post-type/post-name
I managed to remove the CPT from the permalink based on this:
how to remove custom post type from wordpress url?
Then I modify my code to add the custom taxonomy type to the permalink to be like this:
http://example.com/post-category/post-name
This is my code:
function remove_cpt_slug( $post_link, $post ) {
if ( 'custom-post-type-name' === $post->post_type && 'publish' === $post->post_status ) {
$post_tags = get_the_terms($post->ID, 'custom-post-type-name-category');
$post_link = str_replace( '/' . $post->post_type . '/', '/' . $post_tags[0]->slug . '/', $post_link );
}
return $post_link;
}
add_filter( 'post_type_link', 'remove_cpt_slug', 10, 2 );
function add_cpt_post_names_to_main_query( $query ) {
// Return if this is not the main query.
if ( ! $query->is_main_query() ) {
return;
}
// Return if this query doesn't match our very specific rewrite rule.
if ( ! isset( $query->query['page'] ) || 2 !== count( $query->query ) ) {
return;
}
// Return if we're not querying based on the post name.
if ( empty( $query->query['name'] ) ) {
return;
}
// Add CPT to the list of post types WP will include when it queries based on the post name.
$query->set( 'post_type', array( 'post', 'page', 'custom-post-type-name' ) );
}
add_action( 'pre_get_posts', 'add_cpt_post_names_to_main_query' );
This worked but now every post from my CPT gives a 404. How can I solve this?
I want to change the search url to something like this
http://simply.mmag.in/blog/?s=coaching
Where as my current search result shows which is recommended by most user and article
http://simply.mmag.in/blog/coaching
Code which i am using for the above search result
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
and the base url code
function re_rewrite_rules() {
global $wp_rewrite;
$wp_rewrite->search_base = 'blog';
$wp_rewrite->flush_rules();
}
add_action('init', 're_rewrite_rules');
How can i get search result like this
http://simply.mmag.in/blog/?s=coaching
What happends if you just remove or comment out your wpb_change_search_url() function?
Have you tried to just remove this custom code? Wordpress by default uses ?s= in urls, and code used by you just rewrite it.
According to the online ones, adding the following code to functions.php will enable the URL modification.
function tongleer_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'tongleer_search_url_rewrite' );
When I searched for "2015 bmw"
The result obtained is: my-domain.com/search/2015+bmw
It will turn the space into a "+"
How to change the "+" to "-" in the url? And end with add "/". For example. my-domain.com/search/2015-bmw/
Hi I have tried the following code and does seem to work and changes the regular search url for a keyword "holi"
https://www.englishbix.com/search/?s=holi&post_type=product to https://www.englishbix.com/search/holi/
but it uses the default search template to display the product search results instead of the woocommerce search result template.
//change the url to static slug instead of dynamic one
function wpb_change_search_url() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/search/" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'wpb_change_search_url' );
Results I want
Results I get with above code
Wordpress Search
1). www.example.com/?s=perticularword - Not Working
example particular word:"booking"
2). www.example.com/blog/?s=booking - Entered manually in URL it is working
3). If I am using the code - It returns too many redirections - Not Working
function fb_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "/blog/?s=" ) . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'fb_change_search_url_rewrite' );
I want www.example.com/blog/?s=booking this search results how to get results ??
Your if condition is also true for your redirected url. Use
if ( ( is_search() && ! empty( $_GET['s'] ) && stripos( $_SERVER['REQUEST_URI'], 'blog' ) === false ) {
to make the condition false on your redirected url.