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 | |
// Prevent SearchWP from considering repeated terms. | |
add_filter( 'searchwp\tokens\string', function( $string ) { | |
return implode( ' ', array_unique( explode( ' ', strtolower( $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
CREATE TABLE `wp_searchwp_index` ( | |
`indexid` bigint(20) unsigned NOT NULL AUTO_INCREMENT, | |
`token` bigint(20) unsigned NOT NULL COMMENT 'Token ID', | |
`occurrences` bigint(20) unsigned NOT NULL COMMENT 'Number of token occurrences', | |
`id` varchar(255) COLLATE utf8mb4_unicode_520_ci NOT NULL COMMENT 'Source ID', | |
`attribute` varchar(80) COLLATE utf8mb4_unicode_520_ci NOT NULL COMMENT 'Attribute name', | |
`source` varchar(80) COLLATE utf8mb4_unicode_520_ci NOT NULL COMMENT 'Source name', | |
`site` mediumint(9) unsigned NOT NULL DEFAULT '1' COMMENT 'Site ID', | |
PRIMARY KEY (`indexid`), | |
KEY `source_idx` (`source`), |
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 | |
// Randomize SearchWP search results. | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
$mod = new \SearchWP\Mod(); | |
$mod->order_by( 'random', null, 1 ); | |
$mods[] = $mod; | |
return $mods; | |
} ); |
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
<form role="search" method="get" class="search-form" action="<?php echo home_url( '/' ); ?>"> | |
<label> | |
<span class="screen-reader-text"><?php echo _x( 'Search for:', 'label' ) ?></span> | |
<input type="search" class="search-field" | |
placeholder="<?php echo esc_attr_x( 'Search …', 'placeholder' ) ?>" | |
value="<?php echo get_search_query() ?>" name="s" | |
title="<?php echo esc_attr_x( 'Search for:', 'label' ) ?>" /> | |
</label> | |
<p> | |
<input type="checkbox" class="search-field" id="source-post" |
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 | |
// Limit SearchWP results to form field value. | |
// `sources` is a GET array of post type names. | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
if ( empty( $_GET['sources'] ) ) { | |
return $mods; | |
} | |
$mod = new \SearchWP\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 | |
// Limit SearchWP results to Posts and Pages. | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
$mod = new \SearchWP\Mod(); | |
$mod->set_where( [ [ | |
'column' => 'source', | |
'value' => [ | |
\SearchWP\Utils::get_post_type_source_name( 'post' ), |
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 | |
// Give Products extraordinary weight boost to ensure Products show up first. | |
// @link https://searchwp.com/documentation/knowledge-base/post-type-first-top/ | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
$post_type = 'product'; // Post type name. | |
$source = \SearchWP\Utils::get_post_type_source_name( $post_type ); |
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 exclude any posts with a my_meta_key | |
// value of 'meta value 1', 'meta value 2', or 'meta value 3'. | |
add_filter( 'searchwp\post__not_in', function( $ids ) { | |
return array_unique( array_merge( $ids, get_posts( [ | |
'fields' => 'ids', | |
'nopaging' => true, | |
'post_type' => 'any', | |
'meta_query' => [ [ |
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 | |
/** | |
* SearchWP VERSION 4 | |
* Tell SearchWP to index the Title from a Relationship ACF field instead of the post ID | |
*/ | |
add_filter( 'searchwp\source\post\attributes\meta', function( $meta_value, $args ) { | |
$acf_field_name = 'read_next'; // The ACF Relationship field name. | |
// If we're not indexing the Read Next field, return the existing meta value. |
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 | |
// Index non-hyphenated versions of hyphenated strings. | |
add_filter( 'searchwp\tokens\string', function( $string ) { | |
if ( ! did_action( 'searchwp\indexer\batch' ) ) { | |
return $string; | |
} | |
preg_match_all( '/(?=\S*[\-\_])([[:alnum:]\-\_]+)/ius', $string, $hyphenated ); |