Redirect to specific post if no search result is found
-
Hi,
I have the following application to set up (beside Relevanssi, I also use its live-ajax-search companion plugin):
If a user searches for a term for which there is no match, a predefined post should be called up. I found a code snippet in the Relevanssi documentation, which I have adapted (functions.php):
add_action( 'template_redirect', 'one_match_redirect' );
function one_match_redirect() {
if ( is_search() ) {
global $wp_query;
if ( 0 === $wp_query->post_count ) {
wp_redirect( get_permalink( $wp_query->posts['1395']->ID ) );
exit();}}}Unfortunately, just an empty screen is returned instead of the post (ID exists).
What have I done wrong? Maybe a theme-related issue?
Also I am looking for a solution that instead of “Sorry, no results found” during the live search, an entry “Show information about XXX” should be shown that is linked up to the specific post ID (XXX replaces the search term obviously).
Any hint is highly appreciated.
Tobias
- The topic ‘Redirect to specific post if no search result is found’ is closed to new replies.