Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Created Mar 2, 2021
Limit SearchWP Native/Default results to Category that has 'foobar' slug
View searchwp-customizations.php
<?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 Mar 2, 2021
Limit SearchWP results to Category that has 'foobar' slug
View searchwp-customizations.php
<?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 Mar 1, 2021
Tell SearchWP to index Gmedia (Grand Media) Tags alongside Gmedia Albums
View searchwp-customizations.php
<?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 Mar 9, 2021
Control SearchWP AND logic token threshold
View searchwp-customizations.php
<?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 Feb 19, 2021
Disable SearchWP AND logic token threshold, allowing AND logic for all searches.
View searchwp-customizations.php
<?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' );
View searchwp-customizations.php
<?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 Feb 4, 2021
Customize the SearchWP Engine used for Divi Search Results Template
View searchwp-customizations.php
<?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;
@jchristopher
jchristopher / searchwp-customizations.php
Last active Feb 9, 2021
Custom SearchWP Gutenberg Block Parser
View searchwp-customizations.php
<?php
// Disable automatic block parsing during SearchWP index.
add_filter( 'searchwp\source\post\attributes\content\do_blocks', '__return_false' );
// SearchWP custom Block parser.
add_filter( 'searchwp\source\post\attributes\content', function( $content, $args ) {
if ( 'post' === $args['post']->post_type ) {
// This will hold the content we want to index for this Post.
$content_to_index = '';
@jchristopher
jchristopher / searchwp-customizations.php
Created Jan 25, 2021
Customize SearchWP's Indexer throttle
View searchwp-customizations.php
<?php
// Customize SearchWP's Indexer throttle.
add_filter( 'searchwp\background_process\load_throttle',
function( $throttle, $args ) {
return 4 * $args['load'];
},
10, 2 );
@jchristopher
jchristopher / searchwp-customizations.php
Created Jan 25, 2021
Increase SearchWP's load maximum threshold
View searchwp-customizations.php
<?php
// Increase SearchWP's load maximum threshold to allow loads up to 4 instead of 2.
// @link https://searchwp.com/documentation/hooks/searchwp-background_process-load_maximum/
add_filter( 'searchwp\background_process\load_maximum', function( $max ) {
return 4;
} );