The posts_fields
Hook in WPFTS
The posts_fields
hook in WordPress is a filter that allows modification of the list of fields selected from the wp_posts
table in an SQL query. WPFTS uses this hook to add the relev
(relevance) field to the query and to optimize the query in split query mode (split_the_query
).
What WPFTS Does When posts_fields
is Called:
-
Saving the Original Field List: WPFTS saves the original value of
$fields
, passed to the filter, in the session$wpq->wpftsi_session['old_fields']
. This is necessary for correct operation with the split query. -
Determining Split Query Mode: The plugin checks whether to use a split query based on the presence of
LIMIT
in the query and the number of requested posts (posts_per_page
). If a split query is required, WPFTS modifies the list of fields selected in the first query. -
Adding the
relev
Field: In the case of a split query, WPFTS selects only the post ID and its relevance ({$wpdb->posts}.ID, wpfts_t.relev
) in the first query. This significantly speeds up the query, as it does not require selecting all post data. In the second query, all the necessary data for displaying the results is already selected. If a split query is not used, WPFTS simply adds therelev
field to the original list of fields.
Important Functions Involved in posts_fields
Processing:
WPFTS_Search::index_posts_fields()
How to Use This in Addon Development:
Addon developers should be aware that WPFTS can change the list of fields selected in the query. If your addon also modifies this list, ensure that your logic is compatible with WPFTS. In particular, if your addon requires the presence of specific fields in the query, ensure that they are present both when using a split query and without it.
Additional Notes:
-
WPFTS uses a split query to optimize performance with a large number of search results.
-
The
relev
field is added to the query only when index search is enabled.