How to tune search argument in WP_Query to show only exactly the same results?

$args_search = array( 
    's' => $search,
    'exact' => 1,
    'post_type' => array( 'post' )
);

There is no docs in Codex for ‘exact’ param, but best (sure the most reliable) docs is the code itself. See the line 2200 of query.php

A limitation: if you have spaces on the search argument it will not work, because WP_Query consider search term with spaces as different search terms.

So maybe you can use:

$args_search = array( 
    's' => $search,
    'post_type' => array( 'post' )
);
// if no spaces in search we put exact argument
if ( count(explode(' ', $search)) == 1 ) $args_search['exact'] = 1;

A note on your code. Right 'post_type' => array( 'post' ) not ‘posts’ (plural) as you posted.

Leave a Comment