Skip to content

Instantly share code, notes, and snippets.

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`),
@jchristopher
jchristopher / searchwp-customizations.php
Created January 11, 2021 10:24
Randomize SearchWP results
<?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;
} );
<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"
<?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();
<?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' ),
@jchristopher
jchristopher / searchwp-customizations.php
Last active April 22, 2021 20:28
Give Products extraordinary weight boost to ensure Products show up first.
<?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 );
@jchristopher
jchristopher / searchwp-customizations.php
Created December 23, 2020 19:01
Tell SearchWP to ignore any post(s) with a specified meta key value
<?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' => [ [
@jchristopher
jchristopher / searchwp-customizations.php
Last active May 12, 2021 12:38
Tell SearchWP 4 to index the Title from a Relationship ACF field instead of the post ID
<?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.
@jchristopher
jchristopher / searchwp-customizations.php
Created December 17, 2020 19:42
Tell SearchWP to index un-hyphenated versions of hyphenated tokens as well as the hyphenated tokens
<?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 );
@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;