Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created February 25, 2015 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/0caa9d0f14e7e80bec95 to your computer and use it in GitHub Desktop.
Save jchristopher/0caa9d0f14e7e80bec95 to your computer and use it in GitHub Desktop.
Limit SearchWP results to a specific post type
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment