PHP
Filter query args
add_action( 'search-filter/query/query_args', 'search_filter_docs_update_query_args', 10, 2 );
function search_filter_docs_update_query_args( $query_args, $search_filter_query ) {
// Get the query ID.
$query_id = $search_filter_query->get_id();
// If the query ID is 123, update the order.
if ( $query_id === 123 ) {
$query_args[ 'order' ] = 'DESC';
}
// Always return the $query_args.
return $query_args;
}The query class: Search_Filter\Queries\Query
The query instance class.
Function: get_attributes()
Description: Gets all the attributes for a query (most of the settings you can see in the query editor).
Return: associative array of attributes.
Use: $query->get_attributes()
Function: get_attribute( $attribute_name )
Description: Gets a single attribute by name.
Return: attribute value or null if attribute not found.
Use: $query->get_attribute('singleIntegration')
Function: get_id()
Description: Gets the query ID.
Return: the ID.
Use: $query->get_id()
Find a query
Returns: Search_Filter\Queries\Query or a WP_Error if not found.
By name
$query = \Search_Filter\Queries\Query::find( array(
'name' => 'My query', // My query
) );
var_dump($query);By ID
$query = \Search_Filter\Queries\Query::find( array(
'id' => 123,
) );
var_dump($query);Find multiple queries
Returns: array of Search_Filter\Queries\Query
Find queries by status
$queries = \Search_Filter\Queries::find( array(
'status' => 'enabled', // Only get enabled queries.
'number' => 0, // Get all.
) );
var_dump($queries);Find queries by integration type
$queries = \Search_Filter\Queries::find( array(
'status' => 'enabled', // Only get enabled queries.
'number' => 0, // Get all.
'meta_query' => array(
array(
'key' => 'integration_type',
'value' => 'single',
'compare' => '=',
),
),
) );
var_dump($queries);Check if filtering has been applied
// Get the query by ID.
$query = \Search_Filter\Queries\Query::find( array(
'id' => 123, // Replace 123 with your query ID.
) );
$has_filtering = false;
// Get the fields that belong to the query.
$fields = $query->get_fields();
// Loop through all the fields of the query
foreach ( $fields as $field ) {
// Check to see if a value has been set.
if ( ! empty( $field->get_values() ) ) {
$has_filtering = true;
break;
}
}
var_dump( $has_filtering );JavaScript
Get an array of all queries on the page.
const queries = window.searchAndFilter.frontend.queries.getAll();
for ( let i = 0; i < queries.length; i++ ) {
const query = queries[i];
// Get the query ID.
console.log( query.getId() );
// Get query attributes using getAttribute
console.log( query.getAttribute('postTypes') );
console.log( query.getAttribute('resultsDynamicUpdate') );
}Get a single query by ID.
const queryId = 123; // Replace `123` with your query ID.
const query = window.searchAndFilter.frontend.queries.get( queryId );
if ( query ) {
// Get the query ID.
console.log( query.getId() );
// Get query attributes using getAttribute
console.log( query.getAttribute('postTypes') );
console.log( query.getAttribute('resultsDynamicUpdate') );
}Detect start/finish of fetching results
const queryId = 123; // Replace `123` with your query ID.
const query = window.searchAndFilter.frontend.queries.get( queryId );
if ( query ) {
// Only hook in to the update if dynamic update is enabled for the query.
if ( query.getAttribute('resultsDynamicUpdate') === 'yes' ) {
// Listen for the update to the results.
query.on( 'get-results/start', function (queryObject) {
console.log("Start getting results.");
} );
// Listen for the update to the results.
query.on( 'get-results/finish', function (queryObject) {
console.log("Finished getting results.");
} );
}
}