Search code examples
wordpresssearchrelevanssi

Relevanssi Wordpress Search Results


We have a clients website (http://hayball.com.au) running the Relevanssi Wordpress Plugin (https://www.relevanssi.com).

Client has noticed that certain results do not appear when searched for. For example, searching for the term 'learning' should show all results that contain 'learning' as the search term. It does not show the project named "Caulfield Grammar School, Masterplan & Learning Project" even though the search term is in the title.

If you then use "caulfield" as the search term it shows that project. This is the code that displays the search results in the theme.

<?php if (have_posts()): ?>
    <ul>
    <?php while (have_posts()) : the_post(); ?>
        <?php
            $posttype = get_post_type();
            $link = '';
            $readmore = '';
            $excerpt = '';

            if( $posttype == 'projects' ) {
                $category = get_the_terms(get_the_ID(), 'sector');
                $primaryCat = get_post_meta(get_the_ID(),'_yoast_wpseo_primary_sector',true);
                if($primaryCat){
                    $category = null;
                    $primary = get_term($primaryCat, 'sector');
                    $category[] = $primary;
                }
                $link = '<a href="/projects/">' . $project . '</a>';

                if($category) {
                    $link = $link . ' / <a href="/projects/#' . $category[0]->slug . '">' . $category[0]->name . '</a>';
                }
                $link = $link . ' / <a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
                $readmore = get_the_permalink();
                $excerpt = get_the_excerpt();

            }

            if( $posttype == 'news' ) {
                $category = get_the_terms(get_the_ID(), 'category');
                $link = '<a href="/news/">' . $exchange . '</a>';
                if($category) {
                    $link = $link . ' / <a href="/news/#' . $category[0]->slug . '">' . $category[0]->name . '</a>';
                }
                $link = $link . ' / <a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
                $readmore = get_the_permalink();
                $excerpt = get_the_excerpt();
            }

            if( $posttype == 'award' ) {
                $link = ''; //'<a href="/practice/awards/">Awards</a>';
                $readmore = '/practice/awards/';
                $excerpt = get_the_excerpt();
            }

            if( $posttype == 'person' ) {
                $link = ''; //'<a href="/practice/people/">People</a>';
                $readmore = '/practice/people/';
                $excerpt = get_the_title();
            }

            if( $posttype == 'location' ) {
                $link = ''; //'<a href="/contact/">Contact</a>';
                $readmore = '/contact/';
                $excerpt = get_field('address');
            }

            if( $posttype == 'page' ) {
                $link = ''; //'<a href="' . get_the_permalink() . '">' . get_the_title() . '</a>';
                $readmore = get_the_permalink();
            }
        ?>
        <li id="<?php echo get_the_ID(); ?>">
            <div class="content">
                <h4><?php the_title(); ?></h4>
                <p class="link">
                    <?php echo $link; ?>
                </p>
                <p><?php echo $excerpt; ?></p>

                <a href="<?php echo $readmore; ?>"><?php echo translation('read_more'); ?></a>
            </div>

            <?php if(wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'thumbnail'  )) : ?>

            <div class="image" style="background-image: url('<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id(get_the_ID()), 'thumbnail'  ); echo $image[0]; ?>')"></div>
        <?php endif; ?>
        </li>
    <?php endwhile; ?>
    </ul>
    <?php else: ?>
    <p>
        0 <?php echo translation('results'); ?>
    </p>
    <?php endif; ?>

I was also told to try adding this to the functions.php It seems to not have done anything.

function mjt_project_search( $query ) {
if ( $query->is_search ) {
    $query->set( 'post_type', array( 'projects', 'news', 'award', 'person', 'location', 'page') );
    $query->set( 'post_status', array( 'publish') );
}
return $query;}
add_filter( 'pre_get_posts', 'mjt_project_search' );`

When the plugin is disabled it displays the results properly yet the client requires high relevance on all search results, hence the use of the Relevanssi plugin.

Any help or assistance would be greatly appreciated.

Thanks, Dayne


Solution

  • Turns out that the previous developer of the website before our agency took it over neglected to build pagination into the search results. So it did appear but on page 3. Haha.

    Thanks for everyones help.