Posted on

The wrong content gets indexed for a post

Sometimes Relevanssi can get confused and indexes the post content under the wrong post ID. The main symptoms for this are:

  • You can’t find a post with words you know should be in the post
  • Searching for a word returns a post that doesn’t have that word

You can confirm this problem by checking the posts involved with the Relevanssi debugger that shows how Relevanssi has indexed the post. If you have this specific problem, you’ll find that the posts either have no or too little content indexed, or have a lot of content that seems to be from the wrong post.

That’s what’s going on: when Relevanssi indexes a post, it gathers up the content in the post and then stores it in the wp_relevanssi database table under the correct post ID. However, in some cases, the process may get disturbed and the content will end up under the wrong post ID.

The most typical cause for this problem is when you have a post loop element on a page. This may be a block or a shortcode, but it’s something that loops through posts. When Relevanssi sees this element and indexes it, it runs the loop and in some cases, the loop will change the post ID Relevanssi thinks it’s indexing.

Solutions

A simple solution is to exclude the loop element from index. The exact method depends on if it’s a block, a shortcode or something else, but if you exclude it, it won’t confuse Relevanssi anymore.

If this is your shortcode, you can write the code in a way that doesn’t confuse Relevanssi. These problems come when your code changes the global $post value. In general, messing with that is not a good idea, so if your loop code uses new WP_Query(), rewrite it to use get_posts() instead. In most cases, using WP_Query should be restricted to templates and in other cases you should use get_posts().

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

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