Last active
October 20, 2022 10:37
-
-
Save opicron/0ca4207fa7a5c0528fa913b346140874 to your computer and use it in GitHub Desktop.
searchwp custom code #php #searchwp
This file contains hidden or 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
function my_searchwp_pre_search_terms( $terms, $engine ) { | |
$exact_match = get_posts( array( | |
'post_type' => 'product', | |
'nopaging' => true, | |
'fields' => 'ids', | |
'meta_query' => array( | |
array( | |
'key' => '_sku', | |
'value' => $terms, | |
'compare' => 'IN' //LIKE | |
)/*, | |
array( | |
'key' => '_children', | |
'compare' => 'NOT EXISTS' | |
)*/ | |
), | |
)); | |
if( empty( $exact_match ) ){ | |
// remove grouped product and check again..? | |
add_filter( 'searchwp_exclude', 'my_searchwp_exclude', 10, 3 ); | |
} | |
else{ | |
$GLOBALS['swp_exact_sku_match'] = $exact_match; | |
add_filter( 'searchwp_include', 'my_searchwp_include_three_posts', 10, 3 ); | |
} | |
return $terms; | |
} | |
add_filter( 'searchwp_pre_search_terms', 'my_searchwp_pre_search_terms', 10, 2 ); | |
function my_searchwp_exclude( $ids, $engine, $terms ){ | |
// get all grouped product ids from grouped products | |
$grouped_products = get_posts( | |
array( | |
'post_type' => 'product', | |
'nopaging' => true, | |
'fields' => 'ids', | |
'meta_query' => array( | |
array( | |
'key' => '_children', | |
'value' => '', | |
'compare' => '!=' | |
) | |
), | |
) | |
); | |
// get children ids from grouped products | |
$excluded_ids = array(); | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $productID) | |
{ | |
//get _children | |
$product_ids = get_post_meta( $productID, '_children', true ); | |
$excluded_ids = array_merge( $excluded_ids, $product_ids ); | |
} | |
} | |
$ids = array_merge( $ids, $excluded_ids ); | |
return $ids; | |
} | |
function my_searchwp_include_three_posts( $ids, $engine, $terms ) { | |
if( isset( $GLOBALS['swp_exact_sku_match'] ) ){ | |
$ids = $GLOBALS['swp_exact_sku_match']; | |
} | |
return $ids; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment