Posted on

Relevanssi indexes excluded posts

Sometimes it can happen the search results include posts from a post type that has been excluded from the index. When that happens, here are things you can check.

Are you using Relevanssi?

It’s possible your search results are not coming from Relevanssi. The quickest way to check is to use the Relevanssi admin search (Dashboard > Admin search). If you use the admin search and you’re still getting posts from the wrong post type, you can move on to the next step.

If the admin search gets you the correct results but your theme front end search doesn’t, your theme search is not using Relevanssi for some reason.

Common causes for indexing the wrong post types

The most common reason for index including the wrong post type is a shortcode or some other feature that displays posts and confuses Relevanssi.

In the case that prompted this post, the search was showing posts from the publication post type, even though that post type was excluded from the index. The reason was a shortcode, publication_archive, that was used on a page to display an archive of publications.

The shortcode function fetched the posts with WP_Queryand changed the global $post object. That confused Relevanssi and when Relevanssi indexed the page, the post changed in the middle and the wrong post ended up in the index.

Something like this is the most common cause for the wrong post type ending up in the index.

Fixing the problem

In this case the solution was easy: just add the shortcode to the list of disabled shortcodes. Problem solved! This is easy to do in Relevanssi Premium where the list of disabled shortcodes is a simple setting field on the indexing settings page. With the free version, you can adjust this with the relevanssi_disabled_shortcodes filter hook.

If you want to index the shortcode contents, the solution is to modify the code so that it doesn’t touch the global $post object. That is generally the best way to work in any case. So, instead of using WP_Query, use get_posts() and instead of the_post(), just use a local object.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.