No 404 page available

There is 404.php in the theme uploaded. But when there is search for non-existent page. The 404.php does not show up.

When there is search for non-existent page doesn’t produce the 404 page, but simply shows the search.php‘s else portion. A simple search.php is like:

<?php if( have_posts() ) : ?>
   <?php while( have_posts() : the_post(); ?>
      <div><?php the_excerpt(); ?></div>
   <?php endwhile;
<?php else : ?>
   <div><?php _e( "No results found", "text-domain" ); ?></div>
<?php endif; ?>

A search for non-existent pages will show the else part only.

A 404 will generate when something is browsed through URL and that doesn’t exist at all. So at that time the 404.php will be echoed.

If you want to show the else part a more stylish like 404, design the else part as per your demand. 🙂 And if you want the user to redirect to the 404 page (I don’t know why someone can want this!), you can also that like the following (in search.php):

<?php else : ?>
   <?php
   wp_redirect( home_url('/kauwa') ); //kauwa - garbage text that won't produce a real page
   exit;
   ?>
<?php endif; ?>

But as I said, it’s not a good practice, you know.