WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to search via WP_Query for the test "hello " including the last trailing space.

Is this possible and if so, how do I do this?

share|improve this question

If it is possible at all, this is how it would be done:

$searchquery = new WP_Query( array(
    's' => 'hello '

Since all queries boil down to WP_Query at their most basic level, we can determine if it is possible using the WordPress APIs by looking at the code in wp-includes/query.php

// If a search pattern is specified, load the posts that match
if ( !empty($q['s']) ) {
    // added slashes screw with quote grouping when done early, so done later
    $q['s'] = stripslashes($q['s']);
    if ( !empty($q['sentence']) ) {
        $q['search_terms'] = array($q['s']);
    } else {
        preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
        $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
    $n = !empty($q['exact']) ? '' : '%';
    $searchand = '';
    foreach( (array) $q['search_terms'] as $term ) {
        $term = esc_sql( like_escape( $term ) );
        $search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
        $searchand = ' AND ';

    if ( !empty($search) ) {
        $search = " AND ({$search}) ";
        if ( !is_user_logged_in() )
            $search .= " AND ($wpdb->posts.post_password = '') ";

// Allow plugins to contextually add/remove/modify the search section of the database query
$search = apply_filters_ref_array('posts_search', array( $search, &$this ) );

From this we can determine that at multiple stages the search string is stripped and cleaned, and any additional spaces would be removed as part of the sanitisation process.

The only way to conduct such a search would be to reproduce that process using the posts_search filter to reproduce the SQL using your own sanitation process, but I strong recommend against this, for security and maintenance reasons. I would advise whatever system you have, it would be easier to re-architect it than to go down this route, and it could be argued that to do so would be irresponsible as a developer, and non trivial.

Instead I suggest you do your search using a dedicated SQL tool such as workbench, and a query similar to the below:

SELECT * FROM wp_posts WHERE post_content LIKE "hello " LIMIT 0,10
share|improve this answer
Are you sure? I think the space is ignored. – p.a. Dec 11 '12 at 13:40
if it is ignored then the answer is no, it isn't possible. You'll need to implement your own search function with a raw SQL query. Can I ask why you'd want such a feature? – Tom J Nowell Dec 11 '12 at 15:20
I have a legal portal. Posts are laws. One custom field holds the legal references. These are like " law 11 law 17 law 135". – p.a. Dec 11 '12 at 18:49
I allow my users to search legal references custom field for e.g. "law 15" and my code enters leading and trailing spaces so as not to bring "law 151" as well. Am I doing something wrong? – p.a. Dec 11 '12 at 18:51
The legal references may be any large number so it is impossible to enter them in distinct custom fields, so I opted on insering them in a single field. – p.a. Dec 11 '12 at 18:53

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.