# Search Post Titles Only

> Restricts the site search to titles, so results contain only posts whose title matches, which is useful for glossaries, directories and recipe sites.

- Type: PHP
- Runs: Run everywhere
- Category: [Search](https://wpide.com/library/category/search/)
- Tags: search, titles, relevance
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/search-post-titles-only/

## Code

```php
add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() && $query->is_main_query() && $query->is_search() ) {
        $query->set( 'search_columns', array( 'post_title' ) );
    }
} );
```

## Notes

Requires WordPress 6.2 or newer.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Search Post Titles Only" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
