Ask your WordPress questions! Pay money and get answers fast! (more info)

Sort Search by Post Type WordPress

  • SOLVED

I have a filter function here in my functions file. I have been trying to create the correct php to place in the search.php template file to filter search results by post type with a POST TYPE heading at the top of each post type section in search results.

Any thoughts on a solution? This is using the relevanssi_hits_filter found at:
http://www.relevanssi.com/user-manual/relevanssi_hits_filter/

Here is the code in my functions file:


add_filter('relevanssi_hits_filter', 'search_all_types');
function search_all_types($hits) {
$types = array();

$types['page'] = array();
$types['post'] = array();
$types['event'] = array();
$types['documents'] = array();
$types['clients'] = array();
$types['people'] = array();

// Split the post types in array $types
if (!empty($hits)) {
foreach ($hits[0] as $hit) {
array_push($types[$hit->post_type], $hit);
}
}

// Merge back to $hits in the desired order
$hits[0] = array_merge($types['post'], $types['page'], $types['event'],$types['documents'], $types['clients'], $types['people']);
return $hits;
}



The standard search template code is this:


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">

<header>

<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>

<p class="meta"><?php _e("Posted", "wpbootstrap"); ?> <time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time(); ?></time> <?php _e("by", "wpbootstrap"); ?> <?php the_author_posts_link(); ?> <span class="amp">&</span> <?php _e("filed under", "wpbootstrap"); ?> <?php the_category(', '); ?>.</p>

</header> <!-- end article header -->

<section class="post_content">
<?php the_excerpt('<span class="read-more">' . __("Read more on","wpbootstrap") . ' "'.the_title('', '', false).'" &raquo;</span>'); ?>

</section> <!-- end article section -->

<footer>


</footer> <!-- end article footer -->

</article> <!-- end article -->

<?php endwhile; ?>


Answers (1)

2014-04-24

Hariprasad Vijayan answers:

Hello,

Try this code in search template.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
if(empty($current_post_type))
{
$current_post_type = get_post_type( get_the_ID() );
echo $current_post_type;
}
else
{
if($current_post_type != get_post_type( get_the_ID() ))
{
$current_post_type = get_post_type( get_the_ID() );
echo $current_post_type;
}
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p class="meta"><?php _e("Posted", "wpbootstrap"); ?> <time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time(); ?></time> <?php _e("by", "wpbootstrap"); ?> <?php the_author_posts_link(); ?> <span class="amp">&</span> <?php _e("filed under", "wpbootstrap"); ?> <?php the_category(', '); ?>.</p>
</header> <!-- end article header -->
<section class="post_content">
<?php the_excerpt('<span class="read-more">' . __("Read more on","wpbootstrap") . ' "'.the_title('', '', false).'" &raquo;</span>'); ?>
</section> <!-- end article section -->
<footer>
</footer> <!-- end article footer -->
</article> <!-- end article -->
<?php endwhile; ?>


russell comments:

This is giving an "syntax error, unexpected end of file "


To be more clear:
Format of the search results should be as follows:


<h1><CUSTOM POST TYPE A></h1>
LOOP of Custom POSTS

<h1><CUSTOM POST TYPE B></h1>
LOOP of Custom POSTS

<h1><CUSTOM POST TYPE C></h1>
LOOP of Custom POSTS

<h1><CUSTOM POST TYPE D></h1>
LOOP of Custom POSTS







Hariprasad Vijayan comments:

Hi,

endif; is missing. Check this


<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php
if(empty($current_post_type))
{
$current_post_type = get_post_type( get_the_ID() );
echo '<h1>'.$current_post_type.'</h1>';
}
else
{
if($current_post_type != get_post_type( get_the_ID() ))
{
$current_post_type = get_post_type( get_the_ID() );
echo '<h1>'.$current_post_type.'</h1>';
}
}
?>
<article id="post-<?php the_ID(); ?>" <?php post_class('clearfix'); ?> role="article">
<header>
<h3><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
<p class="meta"><?php _e("Posted", "wpbootstrap"); ?> <time datetime="<?php echo the_time('Y-m-j'); ?>" pubdate><?php the_time(); ?></time> <?php _e("by", "wpbootstrap"); ?> <?php the_author_posts_link(); ?> <span class="amp">&</span> <?php _e("filed under", "wpbootstrap"); ?> <?php the_category(', '); ?>.</p>
</header> <!-- end article header -->
<section class="post_content">
<?php the_excerpt('<span class="read-more">' . __("Read more on","wpbootstrap") . ' "'.the_title('', '', false).'" &raquo;</span>'); ?>
</section> <!-- end article section -->
<footer>
</footer> <!-- end article footer -->
</article> <!-- end article -->
<?php endwhile; endif; ?>


Hariprasad Vijayan comments:

Is it working?


russell comments:

Now no errors. but I need it to echo the label and/or a custom name for each post type (not the actual post type name)

Like:

if ($current_post_type ='post') {
echo '<h1>Articles</h1>';
}



if(empty($current_post_type))

{

$current_post_type = get_post_type( get_the_ID() );
echo '<h1>'.$current_post_type.'</h1>';


}




Hariprasad Vijayan comments:

change

$current_post_type = get_post_type( get_the_ID() );
echo '<h1>'.$current_post_type.'</h1>';

to

$current_post_type = get_post_type( get_the_ID() );
$obj = get_post_type_object( $current_post_type );
echo '<h1>'.$obj->labels->singular_name.'</h1>';

Let me know.


Hariprasad Vijayan comments:

Is it fine?


russell comments:

all set, that should work!


russell comments:

all set, that should work!


Hariprasad Vijayan comments:

is that code working?


russell comments:

Yes. Thanks.


russell comments:

Actually, one more issue:
The search function isn't sorting all search results by post type.
Do you know of a way to define the sort order of all searches based on post type?