Separate Posts and Pages in Wordpress Search Results
I recently ran into a problem with Wordpress search. The search results page (search.php) would mix pages and posts together and apply the same styling to each, even claiming that pages were posts by assigning a category to them (pages don't have categories). Searches were also governed by the "Blog pages show at most" option on the Reading Settings page.
I wanted different styling on the pages and posts and to list all search results on one page.
To do this we just run two loops, on the first display only pages, and the second display only posts.
In plain speak:
- if search returns a result
- run through all the pages
- reset query
- run though all the posts
- else return an error message
First let's inform the user what they searched for and how many results were returned.
<h2>Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e('“'); echo $key; _e('”'); _e(' <small>— '); echo $count . ' '; _e('results</small>'); wp_reset_query(); ?></h2>
Credit to www.problogdesign.com for that one.
This will return something along the lines of Search Result for “your search term” — 5 results.
Then we run through the loop,
<?php if (have_posts()) : ?>
but we only want to show the pages at this point, plus we want to make sure the results don't get paged (i.e page 1, page 2 etc.) overriding the default set on our Reader Settings page.
<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=page'"); ?>
The Wordpress query_posts page shows all the arguments available. We're interested in the "posts_per_page=-1" and "post_type=page". '-1' means show all, and 'page' means we're only interested in pages.
Let's have a title and run through the returned pages, if there are any, and a message if there are no page results.
<h3>→ Pages</h3> <?php if ( $query ) : ?> <?php while (have_posts()) : the_post(); ?> <h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2> <?php endwhile; ?> <?php else : ?> No pages found. <?php endif; ?>
What we just said was, if there are any results list them, else give the error message.
Next we need to reset the query.
<?php wp_reset_query(); ?>
Then we repeat the process but for posts only. Note the post type is now "post_type=post".
<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=post'"); ?>
<h3>→ Articles</h3>
<?php if ( $query ) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2>
<span class="author">By: <?php the_author_posts_link(); ?></span>
<span class="post-dates"><?php the_time('F jS, Y') ?></span>
<span class="post-cat"><?php the_category(', ') ?></span>
<div class="entry">
<?php the_excerpt(); ?>
<div class="readmore" ><span class="small">Read Full Article »</span> </div>
</div><!-- !entry -->
</div><!-- !post -->
<?php endwhile; ?>
<?php else : ?>
No articles found.
<?php endif; ?>
If no results were found at all, we'll give them another error message to close off the "<?php if (have_posts()) : ?>" we started with.
<?php else : ?> <p>Sorry, your search didn't return any results.</p> <?php endif; ?>
If we put all that together we end up with:
<h2 class="pagetitle">Search Result for <?php /* Search Count */ $allsearch = &new WP_Query("s=$s&showposts=-1"); $key = wp_specialchars($s, 1); $count = $allsearch->post_count; _e('“'); echo $key; _e('”'); _e(' <small>— '); echo $count . ' '; _e('results</small>'); wp_reset_query(); ?></h2>
<?php if (have_posts()) : ?>
<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=page'"); ?>
<h3>→ Pages</h3>
<?php if ( $query ) : ?>
<?php while (have_posts()) : the_post(); ?>
<h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2>
<?php endwhile; ?>
<?php else : ?>
<p>No pages found.</p>
<?php endif; ?>
<?php wp_reset_query(); ?>
<?php $query = query_posts("$query_string . '&posts_per_page=-1&post_type=post'"); ?>
<h3>→ Articles</h3>
<?php if ( $query ) : ?>
<?php while (have_posts()) : the_post(); ?>
<div class="post">
<h2 class="post-title"><?php edit_post_link('Edit', ' <small>', '</small>'); ?></h2>
<span class="author">By: <?php the_author_posts_link(); ?></span>
<span class="post-dates"><?php the_time('F jS, Y') ?></span>
<span class="post-cat"><?php the_category(', ') ?></span>
<div class="entry">
<?php the_excerpt(); ?>
<div class="readmore" ><span class="small">Read Full Article »</span> </div>
</div><!-- !entry -->
</div><!-- !post -->
<?php endwhile; ?>
<?php else : ?>
<p>No articles found.</p>
<?php endif; ?>
<?php else : ?>
<p>Sorry, your search didn't return any results.</p>
<?php endif; ?>









Comments [0]