Search Results Filter

Search Filter :

Step 1: WordPress Search Result Fillter function

Put this code in function.php

<?php 
//Hook to Search result
function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter'); 
?>

This function is only filer the posts in search results.

Step 2: WordPress Search Result Fillter With Pages functions

<?php 
//Hook to Search result
function SearchFilter($query) {
    if ($query->is_search) {
        $query->set('post_type', 'post', 'page');
    }
    return $query;
}
add_filter('pre_get_posts','SearchFilter'); 
?>

This function is filer the posts and pages in search results.

Leave a Reply

Your email address will not be published. Required fields are marked *