Plugin ArchitectureHooks UsagePosts Search Orderby

posts_search_orderby Hook in WPFTS

The posts_search_orderby hook in WordPress is a filter that allows modifying the ORDER BY part of the SQL query used for sorting search results. WPFTS utilizes this hook to sort search results by relevance if indexed search is enabled and the orderby parameter in the query is not set or is equal to relevance.

What WPFTS Does When posts_search_orderby is Called:

  1. Indexed Search Check: The plugin checks if WPFTS indexed search is enabled for the current query ($wpq->wpftsi_session['use_indexed_search']).
  2. orderby Parameter Check: If indexed search is enabled, the plugin checks if the orderby parameter is set in the query and if it’s equal to relevance or not set at all.
  3. Relevance Sorting: If the conditions are met, WPFTS replaces the standard ORDER BY part with its own, which sorts results by relevance calculated by the plugin’s search engine. The sorting direction (ASC or DESC) is taken from the order parameter of the query.

Important Functions Involved in posts_search_orderby Handling:

  • WPFTS_Search::index_sql_orderby()

How to Use This in Addon Development:

Addon developers should be aware that WPFTS might modify the sorting of search results. If your addon also modifies the ORDER BY part, ensure your logic is compatible with WPFTS.

Additional Notes:

  • WPFTS sorts results by relevance only if indexed search is active for the current query and the orderby parameter is not set or is equal to relevance.
  • Relevance is calculated in the WPFTS_Search::sql_parts() method.

This hook demonstrates how WPFTS manages the sorting of search results and integrates with the standard WordPress mechanism.

Last updated on