Changing search query post_type to “pages” does not affect results

I’m using a theme and was attempting to alter the search query in the functions.php file so that searches would return results from pages rather than posts. The original code in the file was this:

function cryptic_search_filter($query) {
if ($query->is_search && !isset($_GET[‘post_type’])) {
if (!cryptic_plugin_active(‘modeltheme-framework/modeltheme-framework.php’)) {
$query->set(‘post_type’, ‘posts’);
}else{
$query->set(‘post_type’, ‘posts’);
}
}
return $query;
}
add_filter(‘pre_get_posts’,’cryptic_search_filter’);

And I changed it to:

function cryptic_search_filter($query) {
if ($query->is_search && !isset($_GET[‘post_type’])) {
if (!cryptic_plugin_active(‘modeltheme-framework/modeltheme-framework.php’)) {
$query->set(‘post_type’, ‘page’);
}else{
$query->set(‘post_type’, ‘page’);
}
}
return $query;
}
add_filter(‘pre_get_posts’,’cryptic_search_filter’);

But the results are still the same.

What am I doing wrong here?

Read more here:: Changing search query post_type to “pages” does not affect results

Leave a Reply

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