/**
* Piggyback WooCommerce's Layered Navigation and inject SearchWP results where applicable
*
* @param $filtered_posts
*
* @return array
*/
function post_in($filtered_posts)
{
global $wp_query;
// WooCommerce 2.6 introduced tax/meta query piggybacking that's much better
if (function_exists('WC') && !empty(WC()->version) && version_compare(WC()->version, '2.6', '<')) {
return $this->legacy_post_in($filtered_posts);
}
if ($this->is_woocommerce_search() && ($query = get_search_query())) {
if (!empty($this->results)) {
return $this->results;
}
$searchwp_engine = 'default';
$swppg = get_query_var('paged') ? get_query_var('paged') : 1;
// force SearchWP to only consider the filtered posts
if (!empty($filtered_posts)) {
$this->filtered_posts = $filtered_posts;
add_filter('searchwp_include', array($this, 'include_filtered_posts'));
}
do_action('searchwp_woocommerce_before_search', $this);
// don't log this search, it's redundant
add_filter('searchwp_log_search', '__return_false');
$wc_query = new WC_Query();
$args = array('s' => $query, 'engine' => $searchwp_engine, 'page' => $swppg, 'fields' => 'ids', 'posts_per_page' => -1, 'tax_query' => $wc_query->get_tax_query(), 'meta_query' => $wc_query->get_meta_query());
$args = apply_filters('searchwp_woocommerce_query_args', $args);
$results = new SWP_Query($args);
$this->results = $results->posts;
remove_filter('searchwp_log_search', '__return_false');
return $this->results;
} elseif (!empty($this->results)) {
return $this->results;
}
return (array) $filtered_posts;
}