Forums › Forums › Search & Filter Pro › Help with redirect.
- This topic has 6 replies, 3 voices, and was last updated 4 years, 10 months ago by Trevor.
-
Anonymous(Private) December 29, 2019 at 12:12 pm #229950
Sorry to bother, can i please get an adapted code for when search and filter has only one result to redirect to the results post page ?
Meaning if i have just one car post showing to redirect the to the cars page instead of the archive.
Could this code here be adapted somehow to search and filter pro :function sandf_change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET[‘s’] ) ) {
wp_redirect( home_url( “myresultsslug/?_sf_s=” ) . urlencode( get_query_var( ‘s’ ) ) );
exit();
}
}
add_action( ‘template_redirect’, ‘sandf_change_search_url_rewrite’ );Anonymous(Private) December 30, 2019 at 1:33 pm #229982But you talked about this code above to someone on the forums on this link : Topic and it helps him redirect the search results to the home url … why would it be so difficult to do it with a single post result to ” wp_redirect( get_permalink ” i mean redirect to the posts link instead!? Can’t be that hard, i’m sure there are 4 lines of code at max to solve this, i would try to test arguments and functions myself, to make this work, but i really don’t know what to even address inside this plug-ins classes and functions. Can you please get the dev of this plug-in in on the forum line ? This is a very big emergency, my client really needs for single results to redirect to the posts page instead or else i lose the project. I tried to use the code in functions.php :
add_action(‘template_redirect’, ‘single_result’); function single_result() { if (is_search()) { global $wp_query; if ($wp_query->post_count == 1) { wp_redirect( get_permalink( $wp_query->posts[‘0’]->ID ) ); } } }
but that works for the page posts and standard search of wp. I refuse to believe a simple redirect function can’t be easy implemented considering one user of this forum managed to do a redirect with your plug-in for the home page … and believe me i searched the internet and your forum like crazy these past days. Please get the dev to reply.
Ross Moderator(Private) January 2, 2020 at 6:03 pm #230113Hi Alexandru
I can see what you are trying to do, and it would work under the following conditions:
1) You have Ajax disabled
2) Yourdisplay results method
is set toarchive
orpost type archive
(this is because the query needs to be run before the page load, in order for the redirect you want to setup to work)If this is all true, then you could do:
add_action('template_redirect', 'single_result'); function single_result() { global $wp_query; if(!isset($wp_query->query_vars['search_filter_id'])){ return; } $search_filter_id = intval($wp_query->query_vars['search_filter_id']); if($search_filter_id==123){ //you could redirect for a specific S&F ID instead } if ($wp_query->post_count == 1) { wp_redirect( get_permalink( $wp_query->posts[0]->ID ) ); } }
This checks that the search_filter_id has been set on the query, and then redirects based off that.
I’ve tested this locally and it seems to work ok.
Let me know how you get on.
Thanks
Anonymous(Private) January 3, 2020 at 4:57 pm #230201Thank you alot this works wonders, exactly what i needed and it can also be used with a custom template if i set it as archive and check the link a custom made template php + with some grid plug-in shortcode. I will post a video on how to make this work for others on this forum so they can have an easy job implementing this, please don’t close this post yet.
-
AuthorPosts