-
-
Save rsoury/09f9ced91443c42c518c7d49f42bed50 to your computer and use it in GitHub Desktop.
Integrate Flatsome's live search with SearchWP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Standalone function used by Flatesome Search Filter | |
* | |
* @param string $search_query | |
* @param array $args | |
* @param array $defaults | |
* @return array | |
*/ | |
function flatsome_searchwp_search_results($search_query, $args, $defaults) | |
{ | |
$query_args = array_merge($args, array( | |
's' => \SearchWP\Utils::decode_string($args['s']) | |
)); | |
if (!empty($args['posts_per_page'])) { | |
$query_args['posts_per_page'] = absint($args['posts_per_page']); | |
} | |
if (!empty($args['paged'])) { | |
$query_args['page'] = absint($args['paged']); | |
} | |
$search_results = new \SWP_Query($query_args); | |
return $search_results->posts; | |
} | |
add_filter( 'flatsome_ajax_search_function', function flatsome_searchwp_integration($function) | |
{ | |
if (!class_exists('SWP_Query')) { | |
return $function; | |
} | |
return 'flatsome_searchwp_search_results'; | |
}, 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment