Title: posts_search
Published: April 25, 2014
Last modified: April 28, 2025

---

# apply_filters_ref_array( ‘posts_search’, string $search, WP_Query $query )

## In this article

 * [Parameters](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#parameters)
 * [More Information](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#wp--skip-link--target)

Filters the search SQL that is used in the WHERE clause of [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).

## 󠀁[Parameters](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#parameters)󠁿

 `$search`string

Search SQL for WHERE clause.

`$query`[WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)

The current [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/)
object.

## 󠀁[More Information](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#more-information)󠁿

Since version 3.0.0, the `posts_search` filter is used to filter the search SQL 
that is used in the `WHERE` clause of [WP_Query](https://developer.wordpress.org/reference/classes/wp_query/).

## 󠀁[Source](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#source)󠁿

    ```php
    $search = apply_filters_ref_array( 'posts_search', array( $search, &$this ) );
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/class-wp-query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/class-wp-query.php#L2291)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/class-wp-query.php#L2291-L2291)

## 󠀁[Related](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#related)󠁿

| Used by | Description | 
| [WP_Query::get_posts()](https://developer.wordpress.org/reference/classes/wp_query/get_posts/)`wp-includes/class-wp-query.php` |

Retrieves an array of posts based on query variables.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.0.0](https://developer.wordpress.org/reference/since/3.0.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/hooks/posts_search/?output_format=md#comment-content-2213)
 2.   [norrismp](https://profiles.wordpress.org/norrismp/)  [  9 years ago  ](https://developer.wordpress.org/reference/hooks/posts_search/#comment-2213)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_search%2F%23comment-2213)
    Vote results for this note: 12[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_search%2F%23comment-2213)
 4. Example function search by title only:
 5.     ```php
        function wpdocs_search_by_title_only( $search, $wp_query ) {
            global $wpdb;
    
            if ( empty( $search ) ) {
                return $search; // skip processing - no search term in query
            }
    
            $q = $wp_query->query_vars;
            $n = ! empty( $q['exact'] ) ? '' : '%';
            $search = '';
            $searchand = '';
    
            foreach ( (array) $q['search_terms'] as $term ) {
                $term = esc_sql( $wpdb->esc_like( $term ) );
                $search .= "{$searchand}($wpdb->posts.post_title LIKE '{$n}{$term}{$n}')";
                $searchand = ' AND ';
            }
    
            if ( ! empty( $search ) ) {
                $search = " AND ({$search}) ";
                if ( ! is_user_logged_in() )
                    $search .= " AND ($wpdb->posts.post_password = '') ";
            }
    
            return $search;
        }
        add_filter( 'posts_search', 'wpdocs_search_by_title_only', 500, 2 );
        ```
    
 6.  * function __search_by_title_only( $search, &$wp_query ) Should be: function __search_by_title_only(
       $search, $wp_query )
     * [nfong](https://profiles.wordpress.org/nfong/) [6 years ago](https://developer.wordpress.org/reference/hooks/posts_search/#comment-4038)
 7.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_search%2F%3Freplytocom%3D2213%23feedback-editor-2213)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Fhooks%2Fposts_search%2F)
before being able to contribute a note or feedback.