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:

  1. Adding or Removing SQL_NO_CACHE: WPFTS checks the value of the wpfts_nocache option (via $wpq->wpftsi_session['wpfts_nocache']). If this option is enabled (value greater than 0), the plugin adds the SQL_NO_CACHE directive to the DISTINCT keyword. If the option is disabled, the SQL_NO_CACHE directive is removed from the query if it was present. The SQL_NO_CACHE directive 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_CACHE directive based on the wpfts_nocache option.
  • This hook is called for all WP_Query queries, but WPFTS only processes it for search-related queries.