The posts_distinct Hook in WPFTS
The posts_distinct hook in WordPress is a filter that allows modification of the DISTINCT keyword in the SQL query used to retrieve posts. WPFTS utilizes this hook to add or remove the SQL_NO_CACHE directive depending on the plugin’s settings.
What WPFTS Does When posts_distinct is Called:
- Adding or Removing SQL_NO_CACHE: WPFTS checks the value of thewpfts_nocacheoption (via$wpq->wpftsi_session['wpfts_nocache']). If this option is enabled (value greater than 0), the plugin adds theSQL_NO_CACHEdirective to theDISTINCTkeyword. If the option is disabled, theSQL_NO_CACHEdirective is removed from the query if it was present. TheSQL_NO_CACHEdirective instructs MySQL not to cache the query results, which can be beneficial during debugging or with frequently changing data. However, this can decrease performance. Therefore, by default, this option is disabled, and WPFTS removes the directive from the query.
Important Functions Involved in posts_distinct Handling:
- WPFTS_Search::index_posts_distinct()
How to Use This in Addon Development:
Addon developers should be aware that WPFTS might add or remove the SQL_NO_CACHE directive in the query. If your addon also works with this directive, ensure your logic does not conflict with WPFTS.
Additional Notes:
- WPFTS manages the SQL_NO_CACHEdirective based on thewpfts_nocacheoption.
- This hook is called for all WP_Queryqueries, but WPFTS only processes it for search-related queries.