Skip to content

Instantly share code, notes, and snippets.

<?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 );
} );
@jchristopher
jchristopher / searchwp-customizations.php
Created May 22, 2020 23:49
Sort SearchWP WP_Post Results alphabetically by Title in ascending order
<?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;
@jchristopher
jchristopher / searchwp-customizations.php
Last active January 8, 2021 10:14
Add WooCommerce Product (and Variation) SKUs to SearchWP
<?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;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active June 9, 2021 01:00
Make WooCommerce Order Notes searchable by SearchWP
<?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' ] );
}
} );
@jchristopher
jchristopher / searchwp-customizations.php
Last active May 22, 2020 14:29
Add WooCommerce Order Numbers to Orders in SearchWP
<?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;
@jchristopher
jchristopher / searchwp-customizations.php
Created May 22, 2020 13:45
Add WooCommerce Orders as a SearchWP Source.
<?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;
} );
@jchristopher
jchristopher / functions.php
Created May 14, 2020 23:36
Prevent JetSmartFilters from overriding SearchWP's results
<?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' );
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active February 23, 2021 02:12
Adding extra data to indexed entries in SearchWP
<?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:
@jchristopher
jchristopher / searchwp-customizations.php
Created April 22, 2020 18:44
Add relevance weight to a single SearchWP Source
<?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;
@jchristopher
jchristopher / searchwp-customizations.php
Last active November 17, 2021 09:47
Group SearchWP results by Source, sort by relevance within each Source group
<?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',