Skip to content

Instantly share code, notes, and snippets.

@opicron
Created October 10, 2024 10:57
Show Gist options
  • Save opicron/cc2dc7f6859f9ab409a384ff17b4084c to your computer and use it in GitHub Desktop.
Save opicron/cc2dc7f6859f9ab409a384ff17b4084c to your computer and use it in GitHub Desktop.
custom config SearchWP
<?php
/*
// Add additional content to Custom Field during indexing in SearchWP.
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $args ) {
//error_log($args['meta_key']);
if ( 'searchwp_gtin' !== $args['meta_key'] ) {
return $meta_value;
}
error_log('gtin');
// Add 'coffee' to Custom Field value to make it searchable.
return 'coffee';
}, 20, 2 );
*/
// Reduce SearchWP's batch size for token generation.
/*
add_filter( 'searchwp\index\tokens_max', function( $limit ) {
return 10;
} );
*/
// Remove company name "ACME" from tokens in SearchWP.
add_filter( 'searchwp\tokens', function( $tokens ) {
$newtokens = array();
foreach ($tokens as $key => $token)
{
if ( str_contains($token, " ") )
{
array_merge( $newtokens, explode(' ', $tokens));
}
else
{
$newtokens[] = $token;
}
}
//$newtokens[0] = 'lantern';
return $newtokens;
/*
return array_filter( $tokens, function( $token ) {
return 'nan' !== $token;
} );
*/
} );
// Programmatically enable partial matching in SearchWP.
//add_filter( 'searchwp\query\partial_matches', '__return_false' );
// make sure woocommerce doesnt redirect
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );
//
//
////add_filter( 'facetwp_use_search_relevancy', '__return_true' );
// Customize the results returned by SearchWP.
/*
add_filter( 'searchwp\query\results', function( $results, $query ) {
// This is where you can manipulate the $results array of results.
if (is_array($results))
{
//error_log(print_r($results,true));
if (count($results) == 1)
{
//$productid = array_pop($results);
$product = wc_get_product( $results[0] );
$permalink = $product->get_permalink();
//error_log($permalink);
//wp_redirect( $permalink );
}
}
return $results;
}, 30, 2 );
*/
/*
add_filter('searchwp\query\partial_matches\fuzzy\threshold', function ( $threshold ) {
return 100;
});
*/
// Disable SearchWP's fuzzy logic when performing partial matches.
//add_filter( 'searchwp\query\partial_matches\fuzzy', '__return_false' ); //avoid lots of 2 char strings on FZ-60EZ for example
//
// Prevent partial matching on the 'default' SearchWP engine only.
// NOTE: the engine name in this hook tag is: default
//add_filter( 'searchwp_partial_matching_default', '__return_false' );
// Disable keyword stemming for all Engines in SearchWP.
//add_filter( 'searchwp\query\tokens\use_stems', '__return_false' );
// Disable SearchWP's exact match buoy when performing partial match logic.
//add_filter( 'searchwp\query\partial_matches\buoy', '__return_true' );
// // &amp;
// Control SearchWP AND logic token threshold.
// @link https://searchwp.com/documentation/hooks/searchwp-query-logic-and-token_threshold/
add_filter( 'searchwp\query\logic\and\token_threshold', function( $threshold, $args ) {
/*
// Control SearchWP AND logic token threshold.
// @link https://searchwp.com/documentation/hooks/searchwp-query-logic-and-token_threshold/
add_filter( 'searchwp\query\logic\and\token_threshold', function( $threshold, $args ) {
// If the search contains 'coffee' allow up to 10 tokens for AND logic.
if ( in_array( 'coffee', $args['tokens'], true ) ) {
$threshold = 10;
}
// If the search contains 'soccer' disable AND logic token threshold.
if ( in_array( 'soccer', $args['tokens'], true ) ) {
$threshold = false;
}
*/
$threshold = 5;
return $threshold;
}, 20, 2 );
// Prevent specific Category from being indexed with posts in SearchWP.
//
add_filter( 'searchwp\source\post\attributes\taxonomy\terms', function( $terms, $args ) {
// Do not index the '_sap' term.
if (!is_array($terms))
return $terms;
return array_filter( $terms, function( $term ) {
return false === stripos($term->name, '_sap');
//return '_sap-tripod-photo' !== $term->name;
} );
}, 20, 2 );
//Control whether exact matches are given extra relevance weight when performing partial match logic.
// // Enable SearchWP's exact match buoy when performing partial match logic.
//add_filter( 'searchwp\query\partial_matches\buoy', '__return_true' );
// Tell SearchWP to skip rule application for regex pattern matches.
//add_filter( 'searchwp\tokens\apply_rules_to_pattern_matches', '__return_false' );
//this generated way too many results for search term Nano4S
// Remove company name "ACME" from tokens in SearchWP.
add_filter( 'searchwp\tokens', function( $tokens ) {
foreach ($tokens as $key => $token)
{
if ($token[0] == '-')
{
//$tokens[$key] = str_replace('-','',$token);
$tokens[$key] = ltrim($token, '-');
}
if ($token[strlen($token)-1] == '-')
{
//$tokens[$key] = str_replace('-','',$token);
$tokens[$key] = rtrim($token, '-');
}
}
return $tokens;
//return array_filter( $tokens, function( $token ) {
// return 'ii' !== $token;
//} );
} );
/*
// Force partial matches in SearchWP even when matches are found using provided search.
add_filter( 'searchwp\query\partial_matches\force', function( $force, $args ) {
// BY ADDING MORE PATTERNS WE CAN SAFELY TURN THIS OFF!
return false;
}, 10, 2 );
*/
// Tell SearchWP to use a wildcard prefix when performing partial match logic.
//add_filter( 'searchwp\query\partial_matches\wildcard_before', '__return_true' );
// Tell SearchWP to use a wildcard prefix when performing partial match logic.
add_filter( 'searchwp\query\partial_matches\wildcard_after', '__return_true' );
// Force SearchWP to use AND logic and not fall back to OR logic.
//add_filter( 'searchwp\query\logic\and\strict', '__return_true' );
// Enable regex pattern match tokenization in SearchWP.
//add_filter( 'searchwp\tokens\tokenize_pattern_matches', '__return_false' );
// Reduce SearchWP's minimum character length to 2 (default is 3).
add_filter( 'searchwp\tokens\minimum_length', function( $min ) {
return 2;
} );
// Accept up to 12 search terms in SearchWP.
add_filter( 'searchwp\query\logic\and\token_threshold', function( $max, $query ) {
return 8;
//at 70 pavotube 30x doesnt return correct results
//at 400 si-t returns a lot of lenses!
}, 30, 2 );
// Accept up to 12 search terms in SearchWP.
add_filter( 'searchwp\query\tokens\limit', function( $max, $query ) {
return 100;
//at 70 pavotube 30x doesnt return correct results
//at 400 si-t returns a lot of lenses!
}, 30, 2 );
// Allow partial regex pattern matches to be considered in SearchWP.
//add_filter( 'searchwp\tokens\regex_patterns\only_full_matches', '__return_true' );
add_filter( 'searchwp\tokens\regex_patterns', function( $patterns ){
$my_patterns = array(
//"/(?!^[a-zA-Z]{2,3}[\-])([a-zA-Z0-9]{2,20})/UsA", // PT6CII
//
"/(coffee)/",
"/(?!:^[a-zA-Z]{2,3})([-][a-zA-Z0-9\.\-\/]{2}?)/",
"/(?!:^[a-zA-Z]{2,3})([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4,20})/", // HO-IRND1000P82, NL-FZ
"/([a-zA-Z]{1,2})([0-9]{1,4})/", //FZ720
"/(?<=[0-9].)([a-zA-Z]{2,6}\b)/", // 2-4 chars if lookahead is number and end of word
"/([0-9]{1,2}\/[0-9]{1,2})/", // 1/4 3/8 etc
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{6})(?=[0-9].)/", // full code if followed by number // with lookahead
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{5})(?=[0-9].)/", // full code if followed by number // with lookahead
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{4})(?=[0-9].)/", // full code if followed by number // with lookahead
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{3})(?=[0-9].)/", // full code if followed by number // with lookahead
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{2})(?=[0-9].)/", // full code if followed by number // with lookahead
//"/(^[a-zA-Z]{2}[-][a-zA-Z]{1})(?=[0-9].)/", // full code if followed by number // with lookahead
"/([a-zA-Z]{2,4}[0-9]{1}[a-zA-Z]{1})/", //NANOxS / VA5X
"/([a-zA-Z]{2,4}[0-9]{1})/", //NANOx (VA5 WERKT HIER NIET!?)
"/(^[a-zA-Z]{2,3}[-][a-zA-Z]{1,2}[0-9]{2,4})/", // SI-T1004 /
"/([a-zA-Z]{1}[0-9]{4})/", //T1004 N2004
"/([0-9]{2,4})/", //324 / 1004 / 30
"/([0-9]{2,4}[a-zA-Z]{2,3})/", //300BII / 300II / 60BII
"/([0-9]{2,4}[a-zA-Z]{1,1})/", //30C
//"(\b[a-z]{1,2}[0-9]{1,4})", //FZ500 - NOT WORKING
//"([a-z]{1,2}[0-9]{1,4})", //FZ500 - NOT WORKING
"/([a-zA-Z]{1,2})([0-9]{1,4})([a-zA-Z]{1,3})/", //FZ500BII
//"/(?<![a-zA-Z]{1,2})([0-9]{1,4})/" //FZ720
#"([0-9]{1,}[A-Za-z]{1,})" #/iu #for 600w etc
);
//unset($patterns[9]);
/*
foreach ($patterns as $key => $value)
{
if ($value == '/([A-Za-z0-9]{1,}\s[0-9]{1,}[A-Za-z0-9]*)/iu'
||
$value == '(?=\S*[\-\_])([[:alnum:]\-\_]+)')
{
unset($patterns[$key]);
}
}
*/
//unset($patterm)
//print_r($patterns,false);
// we want our pattern to be considered the most specific
// so that false positive matches do not interfere
return array_merge( $my_patterns, $patterns );
//return $my_patterns;
//return array_merge( $my_patterns );
} );
add_filter( 'searchwp\valid_html_tags', function( $tags ) {
if( isset( $tags['img'] ) ){
//Remove whole image tags from the index
unset( $tags['img'] );
}
return $tags;
} );
// Exclude CPL / PLC group filters
// Exclude products that are part of a grouped product
add_filter( 'searchwp\post__not_in', function( $ids ) {
global $wpdb;
$products = $wpdb->get_results( "
SELECT DISTINCT
pm.meta_value as child_ids
FROM
wp_tse_posts AS post
INNER JOIN wp_tse_postmeta AS pm ON post.ID = pm.post_id AND pm.meta_key = '_children'
INNER JOIN wp_tse_postmeta AS pm2 ON post.ID = pm2.post_id AND pm2.meta_key = '_sku'
WHERE
post.post_type = 'product'
AND pm.meta_value > ''
AND pm2.meta_value LIKE 'HO-%%'
ORDER BY
post.id;
");
$children = [];
foreach( $products as $product ){
$ids = maybe_unserialize( $product->child_ids );
if (count($ids) > 1)
$children = array_merge( $children, $ids );
}
return array_merge( $ids, array_merge( $children , [ 2717 , 2368 ] ) );
}, 900, 1 );
// 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';
$my_extra_ean_key = 'searchwp_gtin';
// Retrieve this Product SKU.
$data['meta'][ $my_extra_meta_key ] = [
get_post_meta( $entry->get_id(), '_sku', true )
];
$data['meta'][ $my_extra_ean_key ] = [
get_post_meta( $entry->get_id(), '_gtin', true )
];
// Retrieve all grouped product for this parent.
$product = wc_get_product( $entry->get_id() );
$grouped_products = $product->get_children();
foreach ( $grouped_products as $grouped_product ) {
$sku = get_post_meta( $grouped_product, '_sku', true );
$gtin = get_post_meta( $grouped_product, '_gtin', true );
$data['meta'][ $my_extra_meta_key ][] = $sku;
$data['meta'][ $my_extra_ean_key ][] = $gtin;
}
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();
} );
$keys[] = $option;
$my_extra_ean_key = 'searchwp_gtin';
$option = new \SearchWP\Option( $my_extra_ean_key, 'SearchWP WooCommerce EANs' );
// If there's already a match, remove it because we want ours there.
$keys = array_filter( $keys, function( $option ) use ( $my_extra_ean_key ) {
return $my_extra_ean_key !== $option->get_value();
} );
// Add "SearchWP WooCommerce SKUs" Option
$keys[] = $option;
return $keys;
}, 20, 2 );
/*
function my_searchwp_term_pattern_whitelist( $whitelist ) {
$my_whitelist = array(
"/\\b(IT)\\b/u", // always keep "IT" (all caps only!)
);
// we want our pattern to be considered the most specific
// so that false positive matches do not interfere
$whitelist = array_merge( $my_whitelist, $whitelist );
return $whitelist;
}
*/
//add_filter( 'searchwp_term_pattern_whitelist', 'my_searchwp_term_pattern_whitelist' );
/*
$product_query = new WC_Product_Query([
'type' => ['grouped'],
'limit' => -1
]);
$grouped_products = $product_query->get_products();
*/
// The SQL query
//
/*
$products = $wpdb->get_results( "
SELECT pm.meta_value as child_ids, pm.post_id
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}term_relationships as tr ON pm.post_id = tr.object_id
INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND t.slug LIKE 'grouped'
AND pm.meta_key LIKE '_children'
AND pm.meta_value NOT LIKE '%%63250%%'
ORDER BY p.ID
" );
*/
/*
SELECT pm.meta_value as child_ids, pm.post_id
FROM {$wpdb->prefix}postmeta as pm
INNER JOIN {$wpdb->prefix}posts as p ON pm.post_id = p.ID
INNER JOIN {$wpdb->prefix}term_relationships as tr ON pm.post_id = tr.object_id
INNER JOIN {$wpdb->prefix}terms as t ON tr.term_taxonomy_id = t.term_id
INNER JOIN {$wpdb->prefix}postmeta as pm2 ON pm2.post_id = p.ID AND pm2.meta_key = '_sku'
WHERE p.post_type LIKE 'product'
AND p.post_status LIKE 'publish'
AND t.slug LIKE 'grouped'
AND pm.meta_key LIKE '_children'
AND pm2.meta_value LIKE 'HO-%%'
ORDER BY p.ID
*/
/*
foreach( maybe_unserialize( $result->child_ids ) as $child_id )
if( $child_id == $post_id ){
$parent_grouped_id = $result->post_id;
break;
}
if( $parent_grouped_id != 0 ) break;
*/
//AND t.slug LIKE 'grouped'
//AND p.post_type = 'product_grouped'
/*
foreach ($children as $key => $child)
{
$product = wc_get_product( $child );
if ( substr_compare($product->get_sku, "HO-", 0) !== 0 )
{
unset($children[$key]);
}
}
*/
/*
foreach ($products as $productid)
{
$product = wc_get_product( $productid );
//$children = array_merge( $children, $product->get_children() );
}
*/
/*
foreach( $grouped_products as $grouped_product ){
$children = array_merge( $children, $grouped_product->get_children() );
}
*/
// Search value and delete
//if(($key = array_search(63250, $children)) !== false) {
// unset($children[$key]);
//}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment