Chức năng tìm kiếm trong WordPress mặc định sẽ duyệt tất cả posts và pages. Nhưng thực tế user chỉ muốn search posts, và pages thường dùng cho thông tin chung như About, Contacts… Thế nên, kết quả tìm được sẽ trông khá kỳ dị nếu có pages trong đấy.
Để loại trừ pages khi search, mình thêm đoạn code dưới đây vào file functions.php của theme.
Option 1.
function rewrite_search_query($query) {
if ($query->is_search) {
$query->set('post_type', 'post');
}
return $query;
}
add_filter( 'pre_get_posts', 'rewrite_search_query' );
Option 2.
function rewrite_search_query() {
global $wp_post_types;
$wp_post_types['page']->exclude_from_search = true;
}
add_action( 'init', 'rewrite_search_query' );
Có một vài plugin hỗ trợ việc này:
- Search Exclude by Roman Pronskiy
- Search Everything by Sovrn, Zemanta
- Simply Exclude by Paul Menard