Created
October 6, 2022 08:36
-
-
Save sunilw/8d72455b7640a439ae7e0f6bfdc3503b to your computer and use it in GitHub Desktop.
Limit searches to a specific custom post type
This file contains hidden or 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
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