Comparing Index and Source Mods
\SearchWP\Mod
s are purpose built interactions with SearchWP search queries.
There are two primary types of Mod
s:
- Index-based
- Apply modification to the index as a whole
Source
-based- Limit the modification to a single
Source
The primary way of specifying which type of Mod
you’re implementing is to pass in a Source
(or Source
name
) when instantiating a Mod
.
Note: this is a comparison of the two instantiations, note that these Mod
s don’t actually do anything yet.
<?php | |
// @link https://searchwp.com/documentation/knowledge-base/comparing-index-source-mods/ | |
// Instantiate an index Mod that applies to the entire index. | |
$mod = new \SearchWP\Mod(); | |
// Instantiate a Source Mod that applies only to Posts. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'post' ); | |
$mod = new \SearchWP\Mod( $source ); |
The first Mod
has been instantiated in a way that will apply to the Index as a whole when they query runs. By comparison, the second Mod
will apply only to a single Source
.
Using an Index-based Mod
An Index-based Mod
has only the SearchWP Index as its context. Index-based Mod
s should be limited to those that only need that immediate context to perform their job.
While it is certainly possible to perform additional JOIN
s using an Index-based Mod
, doing so may result in increased query times that may not be so prevalent with a Source
-based Mod
.
For example: an applicable Index-based Mod
would be one that adds ‘bonus’ weight to a particular Source
that is returned as a result.
All hooks should be added to your custom SearchWP Customizations Plugin.
<?php | |
// @link https://searchwp.com/documentation/knowledge-base/comparing-index-source-mods/ | |
// Add relevance weight to a single SearchWP Source (Posts). | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
global $wpdb; | |
$mod = new \SearchWP\Mod(); | |
$source = \SearchWP\Utils::get_post_type_source_name( 'post' ); | |
$mod->weight( $wpdb->prepare( "IF(s.source = %s, 9999999, 0)", $source ) ); | |
$mods[] = $mod; | |
return $mods; | |
} ); |
With this Mod
a ‘bonus’ relevance weight of 9999999
will be afforded to all Posts. A use case for a hook like this may be that you want Posts to be (nearly) guaranteed to show up above all other Source
s without explicitly forcing Source
s to be grouped together.
Using a Source-based Mod
Source-based Mod
s facilitate query modifications that apply only to a single Source
. In this case there already exists a JOIN
with the database table that defines the Source
, which opens new possibilities for modification.
For example: a Source-based Mod
can be used to exclude certain Posts from search results if some sort of condition has been met:
All hooks should be added to your custom SearchWP Customizations Plugin.
<?php | |
// @link https://searchwp.com/documentation/knowledge-base/comparing-index-source-mods/ | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
if ( ! isset( $_GET['my_mod_trigger'] ) ) { | |
return $mods; | |
} | |
// Exclude Posts 97 and 188 from search results. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'post' ); | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'ID', | |
'value' => ['97', '188'], | |
'compare' => 'NOT IN', | |
'type' => 'NUMERIC' | |
] ] ); | |
$mods[] = $mod; | |
return $mods; | |
}, 30, 2 ); |
Using this Mod
Post 97
and 188
are excluded from search results only when $_GET['my_mod_trigger']
has been defined.
Another example: a Source-based Mod
can be used to exclude Pages with the word coffee in their Title:
All hooks should be added to your custom SearchWP Customizations Plugin.
<?php | |
// @link https://searchwp.com/documentation/knowledge-base/comparing-index-source-mods/ | |
add_filter( 'searchwp\query\mods', function( $mods ) { | |
// Exclude Pages with 'coffee' in the Title. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'page' ); | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'post_title', | |
'value' => 'coffee', | |
'compare' => 'LIKE', | |
] ] ); | |
$mods[] = $mod; | |
return $mods; | |
}, 30, 2 ); |
Source-based Mods
are powerful, programmatic ways to modify the way SearchWP performs its searches.