found_posts_query Hook in WPFTS

The found_posts_query filter in WordPress allows modification of the SQL query used to determine the total number of found posts (found_posts) when using pagination. WPFTS utilizes this hook in a specific case when the split query mode (split_the_query) is enabled and it’s necessary to obtain the exact number of found posts considering relevance.

What WPFTS does when found_posts_query is called:

  1. Condition Check: WPFTS doesn’t directly modify the query but relies on standard WordPress logic to determine found_posts using SQL_CALC_FOUND_ROWS in most cases. This filter is used only in the wpq_set_found_posts() method, which, in turn, is called only under certain conditions when using the split query (is_split_query).

  2. Standard Handling: In the case of a split query, WordPress executes two queries: the first to retrieve post IDs and their relevance, the second to retrieve the full post data. The found_posts_query filter is applied to the first query and allows obtaining the exact number of found posts considering the WHERE clause related to relevance.

Important Functions Involved in found_posts_query Handling:

  • WPFTS_Search::wpq_set_found_posts() (internal method, similar to the private method WP_Query::set_found_posts())

  • WPFTS_DB::get_var() (to retrieve found_posts from the database)

How to Use This in Addon Development:

Addon developers interacting with pagination and WPFTS need to understand how the plugin uses found_posts_query in split query mode. If your addon modifies this filter, ensure your logic doesn’t conflict with WPFTS.

Additional Notes:

  • In most cases, WPFTS doesn’t modify the standard query for found_posts.

  • The found_posts_query filter is used only in the specific case of a split query to obtain the exact number of found posts.