• Resolved pccmarkets

    (@pccmarkets)


    I’m working with a custom search template where I create a WP_Query and pass it to relevanssi_do_query(). One of the query args is ‘post__in’ where I pass it an array of posts that I want to restrict the search to. While the results are indeed limited to the posts listed in ‘post__in’, according to the plugin Query Monitor, a large number of queries are generated for all posts matching the search term and not just those defined in ‘post__in’. In particular, the callers with the most queries are update_meta_cache() and WP_Post:get_instance().

    Why is Relevanssi making queries against posts not listed in the ‘post__in’ arg? Is there a way to reduce the number of queries?

    I observe similar behavior when filtering using the ‘post_type’ arg.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter pccmarkets

    (@pccmarkets)

    I believe I determined the source of the problem, which is (of course) not with the Relevanssi plugin but how the custom search template was implemented. Because the custom search template is using the ‘s’ query parameter, the standard/main search query is being executed. But the template is building its own separate query, which is where the ‘post__in’ arg is being passed. I believe both the main search query and the custom one are being executed (hence the unnecessary queries for irrelevant posts), though only the results from the custom query are being displayed.

    I believe using the ‘pre_get_posts’ hook is the proper way to achieve the desired results. If anyone has other thoughts, they are welcome. Thanks!

    Plugin Author Mikko Saari

    (@msaari)

    If there are queries on the search results template, that’s generally a sign of a developer that doesn’t know about the pre_get_posts filter. It’s pretty much always better to just modify the main query instead of running a new query.

    With Relevanssi, you can also use relevanssi_modify_wp_query, which is similar to pre_get_posts, but only applies to Relevanssi searches when pre_get_posts applies to many other cases as well.

    Thread Starter pccmarkets

    (@pccmarkets)

    Thanks Mikko. I see why they went down the path that they did as some of our custom search templates need to function without a search term (no “s” query parameter), but I was able to revise using pre_get_posts, which we were already using. Otherwise, I might have tried relevanssi_modify_wp_query.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Unnecessary queries when using post__in’ is closed to new replies.