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_Query
and 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.
Hello,
I am using a plugin “Get Paid” https://wordpress.org/plugins/invoicing/
I need Relevanssi to include “wpi_invoice” at the search because it is excluded by default. It has “Yes” at “Excluded from search?” column. I need to get search results by searching within custom fields stored in every invoice.
Thanks in advance
Ashraf, you can’t change that value: it comes from the Get Paid plugin, and Relevanssi can’t do anything about it. Relevanssi doesn’t care about it, though; you can index the post type without problems. Uncheck the “Respect ‘exclude_from_search'” setting in the Relevanssi searching settings to make Relevanssi ignore that (but note that it will also open up the invoices to the front-end search – if you don’t want that, you need to control which post types are included in the search and which are not).
Also, there may be some issues in searching post types that the plugin developers are not meant to be searched, it doesn’t always work smoothly if the other plugin makes it hard in some way.
Thank you so much, Mikko. It works.