Skip to content

Instantly share code, notes, and snippets.

@jchristopher
jchristopher / searchwp-customizations.php
Last active June 21, 2021 13:26
Limit SearchWP results to chosen Category from dropdown.
<?php
// Limit SearchWP results to chosen Category from dropdown.
// @link https://searchwp.com/documentation/knowledge-base/category-select-dropdown/
add_filter( 'searchwp\query\mods', function( $mods, $query ) {
global $wpdb;
// Only proceed if a Category was chosen from the dropdown.
if ( ! isset( $_GET['swp_category_limiter'] ) || empty( intval( $_GET['swp_category_limiter'] ) ) ) {
return $mods;
<?php
// Give Pages extraordinary weight boost to ensure Pages show up first.
add_filter( 'searchwp\query\mods', function( $mods ) {
$post_type = 'page';
$source = \SearchWP\Utils::get_post_type_source_name( $post_type );
$mod = new \SearchWP\Mod( $source );
$mod->weight( function( $runtime ) use ( $source ) {
return "IF( {$runtime->get_foreign_alias()}.source = '{$source}', '999999999999', '0' )";
@jchristopher
jchristopher / search-results.php
Last active December 11, 2022 07:43
SearchWP Live Ajax Search results template to output a custom Source (Users in this case) https://searchwp.com/documentation/knowledge-base/custom-source-results-live-search/
<?php
// Execute a search using our supplemental SearchWP Engine.
$search_query = isset( $_REQUEST['s'] ) ? sanitize_text_field( $_REQUEST['s'] ) : null;
$search_results = [];
if ( ! empty( $search_query ) && class_exists( '\\SearchWP\\Query' ) ) {
$searchwp_query = new \SearchWP\Query( $search_query, [
'engine' => 'supplemental', // The Engine name.
'fields' => 'all', // Load proper native objects of each result.
] );
@jchristopher
jchristopher / searchwp-customizations.php
Created November 19, 2020 15:55
Automatically link to WooCommerce Product Variation permalink in SearchWP.
<?php
// @link https://searchwp.com/documentation/knowledge-base/add-woocommerce-product-variations-to-products/
// When using SearchWP it's necessary to disable WooCommerce's insistance on
// automatically redirecting to a single search result without showing the
// search results page, when that happens this hook doesn't run!
// Willing to bet this can be edited to accommodate, tips are welcome!
add_filter( 'woocommerce_redirect_single_search_result', '__return_false' );
<?php
/**
* SearchWP Modal Form Name: Term Archive
*/
?>
<div class="searchwp-modal-form-default">
<div class="searchwp-modal-form__overlay" tabindex="-1" data-searchwp-modal-form-close>
<div class="searchwp-modal-form__container" role="dialog" aria-modal="true">
<main class="searchwp-modal-form__content">
@jchristopher
jchristopher / searchwp-customizations.php
Created November 11, 2020 14:42
Implement a max length for global excerpt generation in SearchWP
<?php
// Implement a max length for global excerpt generation in SearchWP.
add_filter( 'searchwp\source\post\excerpt_haystack', function( $haystack ) {
$max_length = 10000;
if ( strlen( $haystack ) > $max_length ) {
$haystack = substr( $haystack, 0, $max_length );
}
@jchristopher
jchristopher / searchwp-customizations.php
Created November 9, 2020 12:21
Conditionally remove an Engine Source before searching in SearchWP
<?php
// Conditionally remove a SearchWP Engine Source.
add_filter( 'searchwp\query\before', function( $query ) {
// Applies only if `myflag` GET variable exists.
if ( ! isset( $_GET['myflag'] ) ) {
return;
}
// Remove Posts from Engine.
@jchristopher
jchristopher / searchwp-customizations.php
Created October 22, 2020 09:05
Sort SearchWP results in alphabetical order
<?php
// Sort SearchWP results in alphabetical 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', 1 );
$mods[] = $mod;
@jchristopher
jchristopher / searchwp-customizations.php
Created October 7, 2020 13:22
Customize SearchWP stopwords per Engine
<?php
/**
* Customize SearchWP stopwords per Engine.
*/
// Optional: remove all Stopwords so you can add only unique Stopwords per Engine.
add_filter( 'searchwp\stopwords', '__return_empty_array' );
// Add unique stopword(s) for a single SearchWP Engine.
@jchristopher
jchristopher / searchwp-customizations.php
Created October 6, 2020 18:27
Add unique stopword(s) for a single SearchWP Engine
<?php
// Add unique stopword(s) for a single SearchWP Engine.
add_filter( 'searchwp\query\search_string', function( $search_string, $query ) {
// Remove "apple" and "orange" for my_engine searches.
if ( 'my_engine' === $query->get_engine() ) {
$search_string = str_replace( [ 'apple', 'orange' ], '', $search_string );
}
return $search_string;