wpfts_pre_get_posts (Filter)

The wpfts_pre_get_posts filter in WP Fast Total Search is called immediately before the main WP_Query query is executed when using the WPFTS search widget. This allows developers to modify query parameters before execution, providing flexible search customization within the widget.

When to Use

This filter is useful in the following situations:

  • Modifying query parameters based on widget settings: You can modify query parameters such as posts_per_page, post_type, post_status, and others based on the WPFTS widget settings.
  • Adding additional parameters to the query: You can add your own custom parameters to the query, which will be used by your addon or theme.
  • Debugging widget queries: You can use this filter to output query parameters and debug your integration with WPFTS.

Arguments

  • &$wpq (object): The WP_Query object that will be used to execute the query. Passed by reference, so changes made to this object will be preserved.
  • $wdata (array): An array of WPFTS widget settings data. Contains keys such as title, wpfts_wdgt, placeholder, and others. Allows access to the current widget settings to modify query parameters based on those settings.

Return Value

  • The wpfts_pre_get_posts filter should not return any value because the WP_Query object is passed by reference.

Example (Changing the number of posts per page)

 
add_filter( 'wpfts_pre_get_posts', 'my_wpfts_pre_get_posts', 10, 2 );
 
function my_wpfts_pre_get_posts( &$wpq, $wdata ) {
    if ( $wdata['id'] == 'my_custom_widget' ) { // check widget ID
      $wpq->set( 'posts_per_page', 5 ); // set posts per page to 5
    }
  }
 

Important Notes

  • The wpfts_pre_get_posts filter is only called when using the WPFTS search widget.
  • The $wpq object is passed by reference, so any changes made to it within the filter handler will affect the executed query.
  • Be cautious when modifying query parameters, as this can affect the functionality of the widget and plugin as a whole.
  • This filter is called after the plugin has processed the preset settings, so you can override the preset settings within this filter handler.

The wpfts_pre_get_posts filter provides developers with a powerful tool for fine-tuning search queries executed through the WPFTS widget.