searchwp\query\mods
Since: 4.0.0
Table of Contents
Customize the \SearchWP\Mod
s applied during a search.
Note: There are some internal \SearchWP\Mod
s that may be present for the query. This hook does not allow filtration of those mission critical \SearchWP\Mod
s, only user-defined \SearchWP\Mod
s.
See also searchwp\query\do_source_db_where
for internals.
Parameters
Type | Parameter | Default | Since |
---|---|---|---|
\SearchWP\Mod[] | $mods |
[] |
4.0.0 |
\SearchWP\Query | $query |
The Query being run | 4.0.0 |
Examples
All hooks should be added to your custom SearchWP Customizations Plugin.
Build Mod to exclude Post ID 145 and Post ID 211
This file contains 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 | |
add_filter( 'searchwp\query\mods', function( $mods, $query ) { | |
// Retrieve Source name to use with Mod. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'post' ); | |
// Build Mod to exclude Post ID 145 and Post ID 211. | |
$mod = new \SearchWP\Mod( $source ); | |
$mod->set_where( [ [ | |
'column' => 'id', | |
'value' => [ 145, 211 ], | |
'compare' => 'NOT IN', | |
'type' => 'NUMERIC', | |
] ] ); | |
$mods[] = $mod; | |
return $mods; | |
}, 20, 2 ); | |
// Execute search for 'coffee' using Mod enqueued above. | |
$search = new \SearchWP\Query( 'coffee' ); | |
$results = $search->results; // Array of results. |