Created
January 26, 2022 13:29
-
-
Save opicron/33ec7453e02209da8f0747f45ef482e1 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
// Allow partial regex pattern matches to be considered in SearchWP. | |
add_filter( 'searchwp\tokens\regex_patterns\only_full_matches', '__return_false' ); | |
// Enable regex pattern match tokenization in SearchWP. | |
add_filter( 'searchwp\tokens\tokenize_pattern_matches', '__return_true' ); | |
// Tell SearchWP to use a wildcard prefix when performing partial match logic. | |
add_filter( 'searchwp\query\partial_matches\wildcard_before', '__return_false' ); | |
// Tell SearchWP not to use a wildcard prefix when performing partial match logic. | |
add_filter( 'searchwp\query\partial_matches\wildcard_after', '__return_true' ); | |
// Enable debugging in SearchWP. | |
add_filter( 'searchwp\debug', '__return_true' ); | |
// ignore CPL / PLC group filters | |
add_filter( | |
'searchwp\post__not_in', | |
function( $ids ) { | |
return array_merge( $ids, [ 2717 , 2368 ] ); | |
}, | |
20, 2 | |
); | |
// prevent images from being indexed | |
function my_searchwp_prevent_attachment_indexing() { | |
$query_images_args = array( | |
'post_type' => 'attachment', | |
'post_mime_type' =>'image', | |
'post_status' => 'inherit', | |
'posts_per_page' => -1, | |
'fields' => 'ids' | |
); | |
$image_ids = get_posts( $query_images_args ); | |
return $image_ids; | |
} | |
add_filter( 'searchwp_prevent_indexing', 'my_searchwp_prevent_attachment_indexing' ); | |
// Accept up to 12 search terms in SearchWP. | |
add_filter( | |
'searchwp\query\tokens\limit', | |
function( $max, $query ) { | |
return 200; | |
}, | |
30, 2 | |
); | |
// Add WooCommerce Product (and Variation) SKUs to SearchWP. | |
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-skus-and-variation-skus/ | |
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
// If this is not a Product, there's nothing to do. | |
if ( 'product' !== get_post_type( $entry->get_id() ) ) { | |
return $data; | |
} | |
$my_extra_meta_key = 'searchwp_skus'; | |
// Retrieve all Variations. | |
$grouped_products = wc_get_products( | |
array( | |
'type' => 'grouped', | |
'status' => 'publish', | |
'limit' => -1, | |
) | |
); | |
$children = array(); | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $product) | |
{ | |
#$sku = $product->get_sku(); | |
#$data['meta'][ $my_extra_meta_key ][] = $sku; | |
#$product = wc_get_product( $productID ); | |
$children = array_merge($children, $product->get_children()); | |
#echo var_dump($children); | |
//get _children | |
} | |
} | |
#mayb not add grouped child skus | |
// Retrieve this Product SKU. | |
if (! in_array($entry->get_id(), $children)) | |
{ | |
$data['meta'][ $my_extra_meta_key ] = [ | |
get_post_meta( $entry->get_id(), '_sku', true ) | |
]; | |
} | |
/* | |
$product_variations = get_posts( [ | |
'post_type' => 'product_variation', | |
'posts_per_page' => -1, | |
'fields' => 'ids', | |
'post_parent' => $entry->get_id(), | |
] );*/ | |
/* | |
if ( empty( $product_variations ) ) { | |
return $data; | |
} | |
// Append all Product Variation SKUs. | |
foreach ( $product_variations as $product_variation ) { | |
$sku = get_post_meta( $product_variation, '_sku', true ); | |
$data['meta'][ $my_extra_meta_key ][] = $sku; | |
} | |
*/ | |
return $data; | |
}, 20, 2 ); | |
// Add our Extra Meta entry to SearchWP's UI. | |
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-skus-and-variation-skus/ | |
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. | |
$my_extra_meta_key = 'searchwp_skus'; | |
$option = new \SearchWP\Option( $my_extra_meta_key, 'SearchWP WooCommerce SKUs' ); | |
// If there's already a match, remove it because we want ours there. | |
$keys = array_filter( $keys, function( $option ) use ( $my_extra_meta_key ) { | |
return $my_extra_meta_key !== $option->get_value(); | |
} ); | |
// Add "SearchWP WooCommerce SKUs" Option | |
$keys[] = $option; | |
return $keys; | |
}, 20, 2 ); | |
// 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 | |
) | |
), | |
)); | |
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', | |
'status' => 'publish', | |
'limit' => -1, | |
) | |
); | |
$excluded_ids = array(); | |
if (is_array($grouped_products)) | |
{ | |
foreach ($grouped_products as $product) | |
{ | |
#$product = wc_get_product( $product->get_ID() ); | |
$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 #for 1/4 3/8 etc | |
#"([0-9]{1,}[A-Za-z]{1,})" #/iu #for 600w etc | |
); | |
// 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', | |
'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( $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