pre_get_posts
Hook in WPFTS
The pre_get_posts
hook is one of the most important filters in WordPress. It allows modification of WP_Query
parameters before execution. WPFTS uses this hook to integrate its search engine with WordPress and apply search settings defined in presets or passed directly via query parameters.
What WPFTS Does When pre_get_posts
is Called:
-
Checking and Setting the
wpfts_disable
Flag: The plugin checks if thewpfts_disable
flag has been set in the query parameters or plugin settings. This flag allows completely disabling WPFTS search and using the standard WordPress search. -
Determining Search Type and Preset: If WPFTS search is not disabled, the plugin determines the search type (e.g.,
wpmainsearch_admin
,wpmainsearch_frontend
,wpblockquery
) and selects the corresponding preset settings. -
Applying Preset Settings and Query Parameters: WPFTS applies the selected preset settings to the
$wpq
object and processes specific WPFTS query parameters such asword_logic
,orderby
,order
,display_attachments
,limit_mimetypes
,cluster_weights
,wpfts_nocache
, anddeeper_search
. These parameters allow for flexible search customization. -
Processing Results URL for Widgets: If the search is performed through a widget, WPFTS processes the
results_url
parameter and modifies the$wpq
object accordingly so that the search results are displayed on the correct page. -
Calling Filters: WPFTS calls the
wpfts_pre_set_file_search
(deprecated) andwpfts_pre_get_posts
filters, allowing addon developers to modify query parameters. -
Preparing the SQL Query: The plugin prepares the SQL query parts necessary to perform the search, taking into account the settings and parameters. These query parts are then used in other filters such as
posts_search
,posts_join
,posts_orderby
, etc. -
Query Logging: If query logging is enabled, WPFTS logs query information to the database.
Important Functions Involved in pre_get_posts
Processing:
WPFTS_Core::get_option()
WPFTS_Core::GetPresetBySearchType()
WPFTS_Core::GetPresetData()
WPFTS_Core::GetWidgetPresets()
WPFTS_Search::sql_parts()
WPFTS_QueryLog::AfterPreGetPosts()
apply_filters('wpfts_cluster_weights', ...)
do_action_ref_array('wpfts_pre_get_posts', ...)
do_action_ref_array('wpfts_pre_set_file_search', ...)
WP_Query::set()
WP_Query::get()
How to Use This in Addon Development:
Addon developers can use the wpfts_pre_get_posts
filter to modify search query parameters before execution. This allows for flexible integration of addon functionality with WPFTS search.
Additional Notes:
- The
pre_get_posts
hook is called for allWP_Query
requests on the site, but WPFTS only processes it for search-related queries. - WPFTS uses
WP_Query
sessions to store information about the current query. - Processing of query parameters and preset settings occurs in the
WPFTS_Search::index_pre_get_posts()
method. - SQL query preparation is performed in the
WPFTS_Search::sql_parts()
method.
The pre_get_posts
hook is key to WPFTS integration with WordPress and allows developers to flexibly customize search behavior.