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
<?php | |
// Tell SearchWP to index only taxonomy term names instead of the entire WP_Term. | |
add_filter( 'searchwp\source\post\attributes\taxonomy\terms', function( $terms ) { | |
return array_map( function( $term ) { | |
return $term->name; | |
}, $terms ); | |
} ); |
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
<?php | |
// Sort SearchWP WP_Post Results alphabetically by Title in ascending order. | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
$mod = new \SearchWP\Mod( \SearchWP\Utils::get_post_type_source_name( 'post' ) ); | |
$mod->order_by( function( $mod ) { | |
return $mod->get_local_table_alias() . '.post_title'; | |
}, 'ASC', 9 ); | |
$mods[] = $mod; |
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
<?php | |
// 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; | |
} |
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
<?php | |
// Make WooCommerce Order Notes searchable by SearchWP. | |
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
add_action( 'searchwp\source\post\attributes\comments', function() { | |
if ( did_action( 'searchwp\indexer\batch' ) ) { | |
remove_filter( 'comments_clauses', [ 'WC_Comments', 'exclude_order_comments' ] ); | |
} | |
} ); |
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
<?php | |
// Add WooCommerce Order Numbers to WooCommerce Orders. | |
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
$my_extra_meta_key = 'searchwp_order_number'; | |
$my_extra_meta = $entry->get_id() . ' order-' . $entry->get_id() . ' o-' . $entry->get_id(); | |
$data['meta'][ $my_extra_meta_key ] = $my_extra_meta; |
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
<?php | |
// Add WooCommerce Orders as a SearchWP Source. | |
// @link https://searchwp.com/documentation/knowledge-base/search-woocommerce-orders/ | |
add_action( 'plugins_loaded', function() { | |
add_filter( 'searchwp\sources', function( $sources ) { | |
$sources[] = new \SearchWP\Sources\Post( 'shop_order' ); | |
return $sources; | |
} ); |
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
<?php | |
// Prevents JetSmartFilters from overriding SearchWP's results. | |
// @link https://searchwp.com/v3/docs/kb/compatibility-with-jetsmartfilters-for-elementor/ | |
add_action( 'init', function() { | |
add_filter( 'elementor/theme/posts_archive/query_posts/query_vars', function( $query ) { | |
if ( is_search() && is_main_query() ) { | |
remove_all_filters( 'elementor/theme/posts_archive/query_posts/query_vars' ); | |
} |
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
<?php | |
// @link https://searchwp.com/documentation/knowledge-base/adding-extra-data-to-indexed-entries/ | |
// Adding extra data to indexed entries in SearchWP. | |
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) { | |
$my_extra_meta_key = 'my_extra_meta'; | |
// SearchWP can index any data type, array/object values will all be processed. | |
// For this example we will use a string: |
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
<?php | |
// Add relevance weight to a single SearchWP Source. | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
global $wpdb; | |
$mod = new \SearchWP\Mod(); | |
$mod->weight( "IF(s.source = 'user', 9999999, 0)" ); | |
$mods[] = $mod; |
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
<?php | |
// Group SearchWP results by Source, sort by relevance within each Source group. | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
$mod = new \SearchWP\Mod(); | |
$mod->order_by( function( $mod ) { | |
// Search results should be grouped by Sources in this order. | |
// NOTE: _ALL_ Engine Sources must be included here! | |
$source_order = [ | |
'user', |