Enhancing WordPress Search Functionality: Restricting Searches to Posts Only

By Webmaster

Posted: 14m & 15d ago
1 min read

👁 Views: 532

(0) Leave a Comment

Function - WpScriptly

To enhance the search functionality of my WordPress site, I developed a custom functions script that restricts searches to posts only. This specialized script modifies the default WordPress query to exclude pages, attachments, and other custom post types, ensuring that search results are limited to post content. By implementing this script, I aim to streamline the search process for users, providing them with more relevant and targeted results that focus solely on the blog posts. This approach not only improves the user experience by delivering more precise search outcomes but also simplifies content management by honing in on the core content of the site.

I use fluent snippet plugin for all my functions, js, and CSS:

This script will limit the wordpress search to only post types.

if (!is_admin()) {
function search_filter_pages($query) {
if ($query->is_search && $query->is_main_query()) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter('pre_get_posts', 'search_filter_pages');
}

This script is AS IS.

Author: Webmaster

Hey, I’m Scot, the guy behind WpScriptly. I create simple, no-fluff WordPress plugins and tools to make your site easier to manage and more powerful. Everything I build is tested on my own projects first — if it’s here, it’s because it works. Thanks for checking out the site. Hope you find something useful at Wp Scriptly.

Follow me at:

Add your first comment to this post