Thông tin liên hệ

Quốc Trần - Quận Gò Vấp, TP. Hồ Chí Minh

Liên hệ hỗ trợ 0906891294 Zalo
Mạng xã hội
Search post title wordpress
Cá Mập
  • 08/01/2024
  • Blog

To enable a WordPress function that allows users to search exclusively within post titles, you can implement a custom code snippet. Add the following function to your theme’s functions.php file or consider using a site-specific plugin:

How to add code Search post title wordpress

function modify_search_query($query) {
    if ($query->is_search && !is_admin()) {
        $query->set('post_type', 'post'); // Set post type to 'post'
        $query->set('post_status', 'publish'); // Include only published posts
        $query->set('posts_per_page', -1); // Display all matching results
        $query->set('s', ''); // Clear the default search term
        $query->set('order', 'DESC'); // Set the order to descending (latest first)
        $query->set('orderby', 'title'); // Order results by title
    return $query;


This function modifies the default WordPress search behavior by limiting the search to post titles, displaying only published posts, and ordering the results by title in descending order. Users can now enter their search queries in the WordPress search bar, and the function will ensure that the results are refined to match post titles specifically.

Remember to backup your theme files or use a child theme when adding custom functions to maintain the integrity of your WordPress site.

Đánh giá bài viết
Cá Mập

Cá Mập code lập trình wordpress, chia sẽ kiến thức, thủ thuật về web wordpress. Liên hệ làm việc 0906 891 294 ( Phone, Zalo )