Created
January 25, 2022 11:21
-
-
Save opicron/fcbcf6a1ffdd8da1cc63570bc767da37 to your computer and use it in GitHub Desktop.
SearchWP V4
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
// Reduce SearchWP's minimum character length to 2 (default is 3). | |
add_filter( 'searchwp\tokens\minimum_length', function( $min ) { | |
return 2; | |
} ); | |
/*--------------------*/ | |
/* support for parent/child products*/ | |
/* -------------------*/ | |
add_filter( 'searchwp\query\tokens', function( $terms, $query ){ | |
$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\query\mods', 'my_searchwp_exclude', 10, 2 ); | |
} | |
else{ | |
add_filter( 'searchwp\query\mods', 'my_searchwp_include_three_posts', 10, 2 ); | |
$GLOBALS['swp_exact_sku_match'] = $exact_match; | |
} | |
return $terms; | |
}, 10, 2 ); | |
function my_searchwp_exclude( $mods, $query ){ | |
// get all grouped product ids from grouped products | |
$grouped_products = wc_get_products( | |
array( | |
'type' => 'grouped', | |
'fields' => 'id', | |
'status' => 'publish', | |
'limit' => -1, | |
) | |
); | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $product) | |
{ | |
#$product = wc_get_product( $productID ); | |
$children = $product->get_children(); | |
#echo var_dump($children); | |
//get _children | |
#$product_ids = get_post_meta( $productID, '_children', true ); | |
$excluded_ids = array_merge( $excluded_ids, $children ); | |
} | |
} | |
// Retrieve Source name to use with Mod. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'product' ); | |
// Build Mod to exclude Post ID 145 and Post ID 211. | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'id', | |
'value' => $excluded_ids, | |
'compare' => 'NOT IN', | |
'type' => 'NUMERIC', | |
] ] ); | |
$mods[] = $mod; | |
return $mods; | |
} | |
function my_searchwp_include_three_posts( $mods, $query ) { | |
if( isset( $GLOBALS['swp_exact_sku_match'] ) ){ | |
// Retrieve Source name to use with Mod. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'product' ); | |
// Build Mod to exclude Post ID 145 and Post ID 211. | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'id', | |
'value' => $GLOBALS['swp_exact_sku_match'], | |
'compare' => 'IN', | |
'type' => 'NUMERIC', | |
] ] ); | |
$mods[] = $mod; | |
} | |
return $mods; | |
} | |
//Index children SKUs when indexing grouped products | |
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
$post_being_indexed = $entry->native(); | |
if( 'product' == $post_being_indexed->post_type ){ | |
$child_skus = array(); | |
$children = get_post_meta( $post_being_indexed->ID, '_children', true ); | |
if( is_array( $children ) ){ | |
foreach( $children as $child ){ | |
$child_sku = get_post_meta( $child, '_sku', true ); | |
$child_gtin = get_post_meta( $child, '_gtin', true ); | |
$child_skus[] = $child_sku; | |
$child_gtins[] = $child_gtin; | |
} | |
$extra['meta']['child_skus'] = implode( ',', $child_skus ); | |
$extra['meta']['child_gtins'] = implode( ',', $child_gtins ); | |
} | |
} | |
return $data; | |
}, 20, 2 ); | |
// Add 'extra' meta as available option for your Source Attributes. | |
add_filter( 'searchwp\source\attribute\options', function( $keys, $args ) { | |
if ( $args['attribute'] !== 'meta' ) { | |
return $keys; | |
} | |
// This key is the same as the one used in the searchwp\entry\data hook above, they must be the same. | |
$extra_meta_keys = [ | |
'child_skus' => 'Child SKUs', | |
'child_gtins' => 'Child GTIN' | |
]; | |
// Add "Extra Meta" Option if it does not exist already. | |
foreach( $extra_meta_keys as $extra_meta_key => $label ){ | |
if ( ! in_array( | |
$extra_meta_key, | |
array_map( function( $option ) { return $option->get_value(); }, $keys ) | |
) ) { | |
$keys[] = new \SearchWP\Option( $extra_meta_key, $label ); | |
} | |
} | |
return $keys; | |
}, 20, 2 ); | |
add_filter( 'searchwp\tokens\regex_patterns', function( $patterns ){ | |
$my_patterns = array( | |
"/([0-9]{1,2}\\/[0-9]{1,2})/is", | |
); | |
// we want our pattern to be considered the most specific | |
// so that false positive matches do not interfere | |
return array_merge( $my_patterns, $patterns ); | |
} ); | |
/*------------------- | |
Allow certain roles to search draft/schedule/published | |
-------------------*/ | |
function _additional_woo_query( $query ) { | |
if ( is_admin() ) | |
return; | |
if ( (function_exists('is_product_category') && is_product_category()) || is_tax( 'product_brand' ) /*|| is_search()*/ ) | |
{ | |
if (current_user_can( 'administrator' ) || current_user_can('sales_rep') || current_user_can('editor') ) | |
{ | |
$query->set( 'post_status', array('draft', 'publish', 'future') ); | |
} | |
} | |
} | |
add_action( 'pre_get_posts', '_additional_woo_query' ); | |
// Step 1: tell SearchWP to index Drafts and Future in addition to its default post stati. | |
add_filter( 'searchwp\post_stati', function( $post_stati, $args ) { | |
return array_merge( $post_stati, ['draft', 'future'] ); | |
}, 20, 2 ); | |
// Step 2: limit post stati during searches, per post type. By default | |
// SearchWP is going to respect the stati we defined in Step 1! | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
if( current_user_can('administrator') ) | |
{ | |
// admin search | |
$post_status = ['publish', 'draft', 'future']; | |
} else | |
{ | |
// default search | |
$post_status = ['publish']; | |
} | |
foreach ( $query->get_engine()->get_sources() as $source ) { | |
if ( 'post' . SEARCHWP_SEPARATOR !== $source->get_name() ) { | |
continue; | |
} | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'post_status', | |
'value' => $post_status, | |
'compare' => 'IN', | |
] ] ); | |
$mods[] = $mod; | |
} | |
return $mods; | |
}, 20, 2 ); | |
/*--- | |
show drafts/scheduled in archives */ | |
add_filter( 'searchwp_woocommerce_query_args', function( $args ){ | |
if ( is_admin() ) | |
return; | |
if (current_user_can( 'administrator' ) || current_user_can('sales_rep') || current_user_can('editor') ) | |
{ | |
$args['post_status'] = array( 'publish', 'draft', 'future' ); | |
} | |
return $args; | |
} ); | |
add_action( 'pre_get_posts', function( $q ){ | |
if ( is_admin() ) | |
return; | |
if( isset($q->query['post_type']) && $q->is_main_query() && 'product' == $q->query['post_type'] && | |
( | |
current_user_can( 'administrator' ) || current_user_can('sales_rep') || current_user_can('editor') | |
) | |
){ | |
$q->set( 'post_status', array( 'publish', 'draft', 'future' ) ); | |
} | |
}, 10 ); | |
/* | |
// Always ignore Post 732 and Page 98 in SearchWP. | |
add_filter( | |
'searchwp\post__not_in', | |
function( $ids ) { | |
$grouped_products = wc_get_products( | |
array( | |
'type' => 'grouped', | |
'fields' => 'id', | |
'status' => 'publish', | |
'limit' => -1, | |
) | |
); | |
$excluded_ids = array(); | |
// get children ids from grouped products | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $product) | |
{ | |
#$product = wc_get_product( $productID ); | |
$children = $product->get_children(); | |
#echo var_dump($children); | |
//get _children | |
#$product_ids = get_post_meta( $productID, '_children', true ); | |
$excluded_ids = array_merge( $excluded_ids, $children ); | |
} | |
} | |
$ids = array_merge( $ids, $excluded_ids ); | |
return $ids; | |
}, | |
20, 2 | |
); | |
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 | |
), | |
) | |
) | |
); | |
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 = wc_get_products( | |
array( | |
'type' => 'grouped', | |
'fields' => 'id', | |
'status' => 'publish', | |
'limit' => -1, | |
) | |
); | |
$excluded_ids = array(); | |
// get children ids from grouped products | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $product) | |
{ | |
#$product = wc_get_product( $productID ); | |
$children = $product->get_children(); | |
#echo var_dump($children); | |
//get _children | |
#$product_ids = get_post_meta( $productID, '_children', true ); | |
$excluded_ids = array_merge( $excluded_ids, $children ); | |
} | |
} | |
$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