Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Created March 16, 2021 12:54
Enable full wpDataTables table Shortcode parsing when SearchWP indexer is running
<?php
// wpDataTables compatibility with SearchWP. Retrieve entire data table
// if the indexer is running (regardless of wpDataTables settings) to ensure
// all wpDataTables Shortcode content is indexed when parsing Shortcodes.
add_filter( 'wpdatatables_filter_table_metadata', function( $data, $table_id ) {
if ( did_action( 'searchwp\indexer\batch' ) ) {
$data->pagination = false;
$data->showAllRows = true;
$data->server_side = false;
@jchristopher
jchristopher / searchwp-customizations.php
Created March 11, 2021 14:16
Integrate SearchWP with JetSmartFilters search using JetEngine Listing Grid to display results
<?php
// Integrate SearchWP with JetSmartFilters search using
// JetEngine Listing Grid to display results.
add_action( 'pre_get_posts', function( $wp_query ) {
if (
! isset( $wp_query->query['jet_smart_filters' ] )
|| empty( $wp_query->query['s'] )
) {
return;
@jchristopher
jchristopher / searchwp-customizations.php
Created March 3, 2021 19:32
Add secondary sort to SearchWP results to sort matching relevance results by Title in ASC order
<?php
// Add secondary sort to SearchWP results to sort matching
// relevance results by Title in ASC order.
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
$mod = new \SearchWP\Mod();
$mod->set_local_table( $wpdb->posts );
@jchristopher
jchristopher / searchwp-customizations.php
Created March 2, 2021 13:21
Limit SearchWP Native/Default results to Category that has 'foobar' slug
<?php
// Limit SearchWP Native/Default results to Category that has 'foobar' slug.
add_filter( 'searchwp\native\args', function( $args, $query ) {
if ( ! isset( $args['tax_query'] ) || ! is_array( $args['tax_query'] ) ) {
$args['tax_query'] = [];
}
$args['tax_query'][] = [
'taxonomy' => 'category',
@jchristopher
jchristopher / searchwp-customizations.php
Created March 2, 2021 13:11
Limit SearchWP results to Category that has 'foobar' slug
<?php
// Limit SearchWP results to Category that has 'foobar' slug.
$search = new \SWP_Query( [
's' => 'coffee', // Search string.
'tax_query' => [ [
'taxonomy' => 'category',
'field' => 'slug',
'terms' => 'foobar',
] ],
@jchristopher
jchristopher / searchwp-customizations.php
Created March 1, 2021 18:41
Tell SearchWP to index Gmedia (Grand Media) Tags alongside Gmedia Albums
<?php
// Add a SearchWP custom Custom Field to append Gmedia Media Tags to Gmedia Albums when
// indexing, making Gmedia Tags searchable, with Albums coming up for search results.
// @link https://wordpress.org/plugins/grand-media/
add_filter( 'searchwp\entry\data', function( $data, \SearchWP\Entry $entry ) {
global $gmDB, $wpdb;
if ( 'post' . SEARCHWP_SEPARATOR . 'gmedia_album' !== $entry->get_source()->get_name() ) {
return $data;
@jchristopher
jchristopher / searchwp-customizations.php
Last active March 9, 2021 16:53
Control SearchWP AND logic token threshold
<?php
// 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;
}
@jchristopher
jchristopher / searchwp-customizations.php
Last active February 19, 2021 20:47
Disable SearchWP AND logic token threshold, allowing AND logic for all searches.
<?php
// Disable SearchWP AND logic token threshold, allowing AND logic for all searches.
// @link https://searchwp.com/documentation/hooks/searchwp-query-logic-and-token_threshold/
add_filter( 'searchwp\query\logic\and\token_threshold', '__return_false' );
<?php
// Customize SearchWP Engine used.
add_filter( 'searchwp\native\args', function( $args, $query ) {
$args['engine'] = 'supplemental';
return $args;
}, 15, 2 );
@jchristopher
jchristopher / searchwp-customizations.php
Created February 4, 2021 16:02
Customize the SearchWP Engine used for Divi Search Results Template
<?php
// Use `supplemental` Engine for Divi Search Results Templates.
// @link https://searchwp.com/documentation/knowledge-base/divi/
add_filter( 'searchwp\integration\pagebuilder\engine', function( $engine, $params ) {
if ( 'divi' === $params['context'] ) {
$engine = 'supplemental';
}
return $engine;