searchwp\query\before
Since: 4.0.0
Table of Contents
Fires immediately before a \SearchWP\Query
query is executed, before Mod
(s) have been set up.
Parameters
Type | Parameter | Default | Since |
---|---|---|---|
\SearchWP\Query | $query |
The Query being run | 4.0.0 |
Examples
All hooks should be added to your custom SearchWP Customizations Plugin.
Conditinally remove an Engine Source
The following snippet outlines how we can use this hook to conditionally remove the Posts Source from the search Engine if a GET variable is set. If that GET variable is set, Posts will be excluded from search results because the Source has been removed from the Engine.
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 | |
// 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. | |
$source = \SearchWP\Utils::get_post_type_source_name( 'post' ) | |
$query->get_engine()->remove_source( $source ); | |
}, 20, 2 ); |