1

I currently have a sidebar with a search-form at the top. When the user types in a keyword, when submitted the user is took to a page with search results with posts relating to them words in each article.

At the moment, when a keyword is not found, they are sent to a page which shows the title 'nothing found' and a message saying 'Sorry nothing found, Please try again'.

What i would like is whatever page the user is on (Main page, Category page, single page and so forth) the user to stay on that page and just display at the top of the nav this title and information of 'no results found'.

Is this possible at all in some way? I am not the expert when it comes to Wordpress so apologies if the answer is simple and i have not sussed it out!

Thanks.

<?php get_header(); ?>
<?php if ( have_posts() ) : ?>
<?php get_template_part( 'nav', 'above' ); ?>
<h1 class="page-title">
<?php printf( __( 'Search Results for: %s', 'blankslate' ), '<span>' . get_search_query()  . '</span>' ); ?>
</h1>
<?php while ( have_posts() ) : the_post() ?>
<?php get_template_part( 'entry' ); ?>
<?php get_sidebar(); ?>
<?php endwhile; ?>

<?php else : ?>
<?php get_template_part( 'nav', 'above' ); ?>
<h1 class="page-title">
<?php _e( 'Nothing Found', 'blankslate' ) ?>
</h1>
<p><?php _e( 'Sorry, nothing matched your search. Please try again.', 'blankslate' ); ?></p>
<?php get_sidebar(); ?>
<?php get_template_part( 'nav', 'below' ); ?>
<?php get_footer(); ?>
<?php endif; ?>

1 Answer 1

1

Not sure it is a smart thing* to do but it is possible.

The easy solution is to check if you have any posts and if not redirect based on the referer

At the top of your code (before the call to get_header) add

if ( !have_posts() ) { 
  if (isset($_SERVER['HTTP_REFERER'])) {
    wp_redirect(301,$_SERVER['HTTP_REFERER']);
  }
}

This is based on the assumption that a referer is being sent with the request, an assumption which might be false based on browser configuration, and is always false when the site uses SSL.

There is a possible alternative solution in which you change your search form to include the current page URL as a hidden field.

The search form will look like

<form action="<?php echo get_option('siteurl')?>" method="get">
  <input name="s" type="text">
  <input type="hidden" name="ref_url" value="<?php esc_url($_SERVER['REQUEST_URI'])?>">
</form>

And then the code at the top of the search page will be

if ( !have_posts() ) { 
  if (isset($_GET['ref_url'])) {
    wp_redirect($_GET['ref_url'],302); 
  }
} else if (isset($_GET['ref_url'])) { // fix the URL
    wp_redirect(remove_query_arg('ref_url',$_SERVER['REQUEST_URI']),301);    
}

The second part of the code is to remove the extra parameter from a URL otherwise you will have different URLs for the same search based on the page from which the search was conducted.

** It is not a smart thing because people are used to searches which don't find what they look for, but are not used to be redirected to any other place if there are no results. For many it will look as if the search form is not working and you will have to add some notice that there were no results.

4
  • I do understand what you mean about it being not a smart thing, i know i could add it to the top of the header in a banner so people can see it on the redirect but i agree with your point! My only issue with the code i have now is that when no results found, my sidebar,header, ect are loaded but the section where the posts usually are have "untitled", what would you usually load into a search not found, just the search error message? Sorry i am new to blogs/wordpress and thinking of the best way for this type of blog.
    – Shimsham84
    Commented Feb 25, 2013 at 12:47
  • @Shimsham84, perhaps a look at your template for search in your template might solve this untitled problem. Commented Feb 25, 2013 at 13:05
  • @Shimsham84, hard question, but if you want to customize your code for such eventuality you can simply not display the sidebar in that case. Frankly, wordpress search is used very infrequently and its results suck in any case, so unless this is an important feature of a theme I will just leave it as it is. Commented Feb 25, 2013 at 13:32
  • Thank you, the sidebar and content of the blog is much more important than the search function, it's just in the design i was given as an "additional features'. Thanks for your help i will have a think to see what works best in my case.
    – Shimsham84
    Commented Feb 25, 2013 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.