The following is my search.php:
<?php if (have_posts()) : ?>
<h2 class="pagetitle">Page Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type == 'page') : ?>
Show Page results
<?php endif; ?>
<?php endwhile; ?>
<?php rewind_posts(); ?>
<h2 class="pagetitle">Post Search Results</h2>
<?php while (have_posts()) : the_post(); ?>
<?php if ($post->post_type != 'page') : ?>
Show non-page results
<?php endif; ?>
<?php endwhile; ?>
<?php else : ?>
No Results
<?php endif; ?>
The code allows search results under posts to be separated from those found in pages. If no search results are found for either posts or pages, the text “No Results” is displayed.
However, when results are found for pages but NOT posts (and vice versa), under the ‘Post Search Results” there is nothing displayed. I would like the code tweaked so that if there are no results found under “Post Search Results” but there are results found under “Page Search Results,” the text “No Results” is displayed underneath the “Post Search Results” header.
Thanks a lot everyone.
Give this a try, you can expand the array to include any post_type your site is using: