• I am building a site which relies on WordPress as a CMS. The front page is a static page, there are going to be several hundred pages of content as well as several hundred imported blog posts.

    I’ve used a plugin called Search Everything to include pages and metadata in my searches. The problem is that the pages and posts on my site are logically separate, but all come up mixed together on the same search page. Does anyone know of a way to split up the search results so that Posts and Pages are returned separately?

    Alternately, I suppose I could have one search box for Pages, and another search box on the blog that only returns results from Posts. Sounds weird, but that makes more sense than lumping them together in one page. Is it possible?

Viewing 8 replies - 1 through 8 (of 8 total)
  • this sounds like a question for the plugin author.

    wordpress does have this logical separation by simply not allowing you to search pages (not that that’s a good thing).

    Thread Starter Dalton Rooney

    (@daltonrooney)

    Thanks, Ivovic. I’ll post a request to the plugin author and see what they suggest.

    In your search.php file, just after the line <?php while (have_posts()) : the_post(); ?> insert the condition if ( is_post() ).

    Then get the Loop to start again with `<?php while (have_posts()) : the_post(); ?>
    if ( !is_post() )`

    I haven’t tested it, but it seems likely that this would work. Given that WordPress doesn’t expect results to include pages it seems the only way to separate them would be to run the entire loop twice. Let me know if it works!

    Thread Starter Dalton Rooney

    (@daltonrooney)

    Thanks Kalessin, for that. Oddly enough, I get an a fatal error:

    Call to undefined function is_post()

    Has that been deprecated in 2.3? Or am I doing it wrong:

    <?php if (have_posts()) : ?>
    
    <?php while (have_posts()) : the_post(); ?>
    
    <?php if (!is_post()) { ?>
    
    <h3><a>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h3>
    
    <?php the_excerpt(); ?>
    
    <?php } ?>
    
    <?php endwhile; ?>
    
    <?php else : ?>
    
    <h2 class="center">No posts found. Try a different search?</h2>
    <?php include (TEMPLATEPATH . '/searchform.php'); ?>
    <?php endif; ?>

    My bad: try is_page() instead.

    Does anyone know how to accomplish this? Ive tried to find a code example somewhere with no luck. I was hoping to remove the date from my search results when a Page is listed. In my site which is currently running WP 2.5.1 the pages are displayed first but with an annoying date format next to each Page listing. I want the rest of the search results to be displayed exactly how they are now. For Posts, the date is appropriate.
    here is the example search results with Pages and Posts shown together:
    https://kre8.tv/?s=music
    I’ve tried using a conditional is_page inside the loop but with no success. I was thinking I could accomplish this using multiple loops but have not been able to find an example of this type of Page/Post separation inside a search.php template.

    Any tips with code examples would be greatly appreciated! Thanks!

    is_page() is used to describe the page/template the results are on, not the individual results. So it is checking to see if your search results page is_page().

    To look at the individual results you want to look at the $post object that is returned by the query, specifically you want to view the page_type. The complete search.php template would follow this…

    <?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; ?>

    I was not satisfied with the results I got from that. I wanted to show a set number of results per area (5) and this simply returned the top 10 results, which in my case were all pages…
    This lead me to creating custom queries. The only issue I ran into was the next and previous links wouldn’t work.

    Thanks to a comment by Aaron Harun over at: weblogtoolscollection I was able to find a solution to that issue. The new code is as follows:

    <?php
    $page_query = new WP_Query("s=$s&paged=$paged&showposts=5&post_type=page"); //$page_query->query_vars['post_type']='page';//query_posts("s=$s&paged=$paged&cat=-1,-11,-101");
    $post_query = new WP_Query("s=$s&paged=$paged&showposts=5&post_type=post"); //$post_query->query_vars['post_type']='post';//query_posts("s=$s&paged=$paged&cat=-1,-11,-101");
    if ($page_query->have_posts()) :
    	$wp_query= null; $wp_query = clone $page_query;?>
    	<div class="navigation">
    		<div class="left"><?php previous_posts_link(' &laquo; Prev') ?></div>
    		<div class="right"><?php next_posts_link('Next &raquo;'); ?></div>
    	</div>
    
    	<h2 class="pagetitle">Page Search Results</h2>
    	<?php while ($page_query->have_posts()) : $page_query->the_post(); ?>
    		<!-- do stuff -->
    	<?php endwhile; ?>
    <?php endif;?>
    <?php if($post_query->have_posts()) :
    	if(!$page_query->have_posts()) {$wp_query= null; $wp_query = clone $post_query;} ?>
    	<h2 class="pagetitle">Post Search Results</h2>
    	<?php while ($post_query->have_posts()) : $post_query->the_post(); ?>
    		<!-- do stuff -->
    	<?php endwhile; ?>
    	<div class="navigation">
    		<div class="left"><?php previous_posts_link(' &laquo; Prev') ?></div>
       		<div class="right"><?php next_posts_link('Next &raquo;'); ?></div>
    	</div>
      <?endif; ?>
    <?php if (!$page_query->have_posts() && !$post_query->have_posts()) : ?>
    	<h2 class="center">No results were found. Please try a different search?</h2>
    	<?php include (TEMPLATEPATH . '/searchform.php'); ?>
    <?php endif; ?>

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Creating a search page which separates Posts from Pages?’ is closed to new replies.