Split/group search results by post type?

So WordPress default search results/output page (search.php) searches for everything and displays everything. Pages and Posts together in my case.

What I want is to spit these results. I tried it a few ways, but I always run into some kind of an infinite loop, and the page does not want to load at all. I tried to adapt the loop, add post type check and foreach to first output posts, and then pages, but no luck.

Is someone familiair with this or can help me out? Of course I’ll pay for it.

I just reverted the code to the original state which is below. Since I don’t really know what I’m doing anyways..

<section class=”header-section”>
<div class=”page_header” <?php if(mwhost_get_option(‘mwhost_page_header_img’) != ”) { ?>style=”background: rgba(0, 0, 0, 0) url(‘<?php echo esc_url( mwhost_get_option(‘mwhost_page_header_img’) ); ?>’) no-repeat scroll center top;”<?php } ?>>
<div class=”container”>
<h2 class=”align_center pagetitle”>
<?php echo esc_html_e( ‘Search Results’, ‘mwhost’ ); ?>
</h2>
<?php if (function_exists(‘mwhost_wordpress_breadcrumbs’)) mwhost_wordpress_breadcrumbs(); ?>
</div>
</div>

<?php if (have_posts()) { ?>
<section class=”section41″>
<div class=”container”>

<?php $counter = 1; ?>
<?php while (have_posts()) : the_post();
$mwhost_global_post = mwhost_get_global_post();
$postid = $mwhost_global_post->ID;
$get_image = esc_url( wp_get_attachment_url( get_post_thumbnail_id($postid) ) );
?>
<div <?php post_class(); ?> id=”post-<?php the_ID(); ?>”>
<?php if ($counter == 1) { ?>
<div class=”one_full”>
<?php } elseif ($counter == 2) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”100″>
<?php } elseif ($counter == 3) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”200″>
<?php } elseif ($counter == 4) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”300″>
<?php } elseif ($counter == 5) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”400″>
<?php } elseif ($counter == 6) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”500″>
<?php } elseif ($counter == 7) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”600″>
<?php } elseif ($counter == 8) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”700″>
<?php } elseif ($counter == 9) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”800″>
<?php } elseif ($counter == 10) { ?>
<div class=”one_full animate-in” data-anim-type=”fade-in-down” data-anim-delay=”1000″>
<?php } ?>
<?php if ( has_post_thumbnail()) : ?>
<div class=”post_img”>
<div class=”overley”></div>
<div class=”date”> <span><?php echo get_the_time(‘d’); ?></span><?php echo get_the_time(‘M, Y’); ?> </div>
<div class=”date comments”> <span><?php echo get_comments_number(); ?></span><?php esc_html_e(‘Comments’, ‘mwhost’); ?> </div>
<img class=”img-size1″ alt=”” src=”<?php echo esc_url( $get_image ); ?>”>
</div>
<?php endif; ?>
<a href=”<?php the_permalink();?>”>
<h3 class=”uppercase <?php if ( has_post_thumbnail()) : ?>padd_top2<?php endif; ?>”><?php the_title(); ?></h3>
</a>
<div class=”post_info”> <span><i class=”fa fa-folder-open-o”></i> <?php esc_html_e(‘Categories’, ‘mwhost’); ?> / <?php the_category( ‘, ‘ ); ?></span><span><i class=”fa fa-user”></i> <?php esc_html_e(‘By’, ‘mwhost’); ?> <?php the_author(); ?> </span> </div>
<?php if ( has_post_format( ‘video’ ) ) : ?>
<?php the_content(); ?>
<?php else: ?>
<?php the_excerpt(); ?>
<?php endif; ?>

<div class=”readmore”><a href=”<?php the_permalink();?>” class=”btn linebtn”><?php esc_html_e(‘Read more’, ‘mwhost’); ?></a></div>
</div>
</div>
<?php if (($wp_query->current_post +1) != ($wp_query->post_count)) { ?>
<div class=”clearfix margin_top7″></div>
<?php } else { ?>
<div></div>
<?php } ?>
<?php $counter++; ?>
<?php endwhile; ?>
<!–end item–>

<div class=”divider_dashed2″></div>
<div class=”bl_pagenation_holder one”>
<!–Pagination–>
<?php mwhost_custom_pagination(); ?>
<!–End Pagination–>
</div>
<!–Sidebar–>

<!–Sidebar end–>
</div>
</section>

Read more here:: Split/group search results by post type?

Leave a Reply

Your email address will not be published. Required fields are marked *