Skip to content

Instantly share code, notes, and snippets.

@sunilw
Created October 6, 2022 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilw/8d72455b7640a439ae7e0f6bfdc3503b to your computer and use it in GitHub Desktop.
Save sunilw/8d72455b7640a439ae7e0f6bfdc3503b to your computer and use it in GitHub Desktop.
Limit searches to a specific custom post type
function sw_cpt_search($query) {
if (
isset ( $_REQUEST['search'] ) &&
$_REQUEST['search'] == 'landlord' &&
! is_admin() &&
$query -> is_search &&
$query -> is_main_query()
) {
// limit the query to 'landlord' posts
$query->set('post_type', 'landlords');
}
add_filter('pre_get_posts', 'sw_cpt_search');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment