Skip to content

Instantly share code, notes, and snippets.

@emre-edu-tech
Created August 26, 2024 10:33
Show Gist options
  • Save emre-edu-tech/e6b1a980acec4e08c974fe7c742732fc to your computer and use it in GitHub Desktop.
Save emre-edu-tech/e6b1a980acec4e08c974fe7c742732fc to your computer and use it in GitHub Desktop.
Elementor Custom Query Filter using New Search Widget. Search widget supports ajax hitting the Rest Endpoint
<?php
// Here we use tax query but meta_query is also possible
// Unfortunately, there is no way to merge Wordpress default search behaviour, meta_query and tax_query using OR
// that I could find.
// Default behaviour for merging these queries are AND.
// Custom Elementor Query for Search
function mpons_custom_property_search_query($query) {
// Get the search term from the WP_Query object
$search_term = $query->get('s');
// load the terms using LIKE
$termIds = get_terms([
'name__like' => $search_term,
'fields' => 'ids'
]);
// Get current tax Query
$tax_query = $query->get('tax_query');
// If there is no tax query when this filter runs, it should be initialized as an empty array.
if (!$tax_query) {
$tax_query = [];
}
// Append our tax query
$tax_query[] =[
'taxonomy' => 'location',
'field' => 'id',
'terms' => $termIds,
];
// AND relation, there is no way I could find for the OR relation
$query->set('s', $search_term); // not necessary since this is default Wordpress search behaviour, it is here for demonstration
$query->set('tax_query', $tax_query);
}
// Hook the custom query into Elementor
add_action('elementor/query/custom-property-search-query', 'mpons_custom_property_search_query');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment