Skip to content

Instantly share code, notes, and snippets.

@rsoury
Forked from jchristopher/functions.php
Last active November 12, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rsoury/09f9ced91443c42c518c7d49f42bed50 to your computer and use it in GitHub Desktop.
Save rsoury/09f9ced91443c42c518c7d49f42bed50 to your computer and use it in GitHub Desktop.
Integrate Flatsome's live search with SearchWP
<?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