Available since: 1.0
searchwp_where
View Parameters »If you are an advanced developer and would like to directly manipulate the WHERE
clause of the main search algorithm SQL you can use this filter.
Example: To customize the WHERE clause, add the following to your active theme’s functions.php
:
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 | |
function my_searchwp_where( $clause, $engine ) { | |
// manipulate $clause to your liking and return it | |
return $clause; | |
} | |
add_filter( 'searchwp_where', 'my_searchwp_where', 10, 2 ); |
Example: To limit results to a specific post_type
based on a $_GET
variable named post_type
, add the following to your active theme’s functions.php
:
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 | |
function my_searchwp_limit_to_post_type( $clause, $engine ) { | |
global $wpdb; | |
if ( isset( $_GET['post_type'] ) ) { | |
$post_type = sanitize_text_field( $_GET['post_type'] ); | |
if ( post_type_exists( $post_type ) ) { | |
$clause = $wpdb->prepare( "AND {$wpdb->prefix}posts.post_type = %s", $post_type ); | |
} | |
} | |
return $clause; | |
} | |
add_filter( 'searchwp_where', 'my_searchwp_limit_to_post_type', 10, 2 ); |
Parameters
Parameter | Type | Description |
---|---|---|
$clause |
String |
The original |
$engine |
String |
The search engine being used |