How can I restrict the search for a specific post type? I am using the following code in functions.php:

function searchfilter($query) { 
    if ( is_admin() || ! $query->is_main_query() ){return;}

    if( $query->is_main_query()  ) {
       $query->set( 'post_type', ['property']);
     return $query;

I figured it out, thanks

function my_search_pre_get_posts($query)

    if($query->query_vars['s'] != '' && is_search())

            $query->set('p', $query->query_vars['s']);
            $query->set('post_type', ['property']);
            // Reset the search value
            $query->set('s', '');

// Filter the search page
add_filter('pre_get_posts', 'my_search_pre_get_posts');

A very good learning of WordPress functionality.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.