I am having a problem with a WordPress website that uses the Relevanssi search plugin. It seems that some posts are not appearing in the search results even though they should. For instance, when I search for the term “education”, I expect to see a post titled “Melbourne High School - Education & Development Project”, but it doesn’t come up. However, searching for just “melbourne” does show that post.
Here is the template code I’m using for displaying search results:
<?php if (have_posts()): ?>
<div class="search-results">
<?php while (have_posts()) : the_post(); ?>
<?php
$content_type = get_post_type();
$breadcrumb = '';
$view_more = '';
$summary = '';
if( $content_type == 'portfolio' ) {
$terms = get_the_terms(get_the_ID(), 'category');
$main_cat = get_post_meta(get_the_ID(),'_yoast_wpseo_primary_category',true);
if($main_cat){
$terms = null;
$primary_term = get_term($main_cat, 'category');
$terms[] = $primary_term;
}
$breadcrumb = '<a href="/portfolio/">' . $work . '</a>';
if($terms) {
$breadcrumb = $breadcrumb . ' / <a href="/portfolio/#' . $terms[0]->slug . '">' . $terms[0]->name . '</a>';
}
$breadcrumb = $breadcrumb . ' / <a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
$view_more = get_the_permalink();
$summary = get_the_excerpt();
}
if( $content_type == 'articles' ) {
$terms = get_the_terms(get_the_ID(), 'article_category');
$breadcrumb = '<a href="/articles/">' . $blog . '</a>';
if($terms) {
$breadcrumb = $breadcrumb . ' / <a href="/articles/#' . $terms[0]->slug . '">' . $terms[0]->name . '</a>';
}
$breadcrumb = $breadcrumb . ' / <a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
$view_more = get_the_permalink();
$summary = get_the_excerpt();
}
if( $content_type == 'recognition' ) {
$breadcrumb = '';
$view_more = '/about/recognition/';
$summary = get_the_excerpt();
}
if( $content_type == 'staff' ) {
$breadcrumb = '';
$view_more = '/about/team/';
$summary = get_the_title();
}
if( $content_type == 'office' ) {
$breadcrumb = '';
$view_more = '/contact-us/';
$summary = get_field('street_address');
}
if( $content_type == 'page' ) {
$breadcrumb = '';
$view_more = get_the_permalink();
}
?>
<div class="result-item" id="<?php echo get_the_ID(); ?>">
<div class="text-content">
<h3><?php the_title(); ?></h3>
<div class="breadcrumb">
<?php echo $breadcrumb; ?>
</div>
<div class="description"><?php echo $summary; ?></div>
<a href="<?php echo $view_more; ?>">View Details</a>
</div>
<?php if(wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'medium' )) : ?>
<div class="thumbnail" style="background-image: url('<?php $thumb = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'medium' ); echo $thumb[0]; ?>')"></div>
<?php endif; ?>
</div>
<?php endwhile; ?>
</div>
<?php else: ?>
<div class="no-results">
No matches found
</div>
<?php endif; ?>
I also added this function to my functions.php file, but it has not resolved the issue:
function custom_search_filter( $query ) {
if ( $query->is_search ) {
$query->set( 'post_type', array( 'portfolio', 'articles', 'recognition', 'staff', 'office', 'page') );
$query->set( 'post_status', array( 'publish') );
}
return $query;
}
add_filter( 'pre_get_posts', 'custom_search_filter' );
When I disable the Relevanssi plugin, the search functions correctly and shows all matching posts. However, I want to use Relevanssi because it improves the relevance of the search results. Has anyone else experienced this issue? Any suggestions on what might be the reason certain posts are not appearing in the Relevanssi search results?