How to find exact match for search term in WP_Query? What is the additional string added in LIKE query in WP_Query?

The question:

While searching with the argument s it adds additional value before and after each Like query

SELECT wp_7_posts.ID FROM wp_7_posts WHERE 1=1 AND (((wp_7_posts.post_title LIKE '{d146fc7cb41ff444163abadd50ac5da7ec5439fd51386c3cfe2cea107126703b}acetic{d146fc7cb41ff444163abadd50ac5da7ec5439fd51386c3cfe2cea107126703b}')

Is there anyway to search exact term rather than searching word by word using WP_Query?

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

it adds additional value before and after each Like query

That {d146fc7cb41ff444163abadd50ac5da7ec5439fd51386c3cfe2cea107126703b} is a placeholder escape string for the literal % (percent) sign used in prepared statements (see wpdb::prepare()) and the escape string is generated by wpdb::placeholder_escape().

So actually, that {d146fc7cb41ff444163abadd50ac5da7ec5439fd51386c3cfe2cea107126703b} is the % sign, but in an escaped form, and it will be changed back to % when WordPress sends the query to MySQL.

Therefore don’t worry about that value; it’ perfectly valid and added for a good reason.

Is there anyway to search exact term

Yes, there is: Use the exact argument and set it to 1 or true. E.g.

  1. In the browser, navigate to

  2. Or in your template/PHP, run new WP_Query( 's=acetic&exact=1' ).

… and both the above will lead to WordPress searching for posts where for example the post title is exactly acetic and not acetic acid. I.e. The LIKE clause will not use the % sign as in post_title LIKE 'acetic' as opposed to post_title LIKE '%acetic%'.

Additionally, if you want to preserve spaces in the search keyword, wrap it with double quotes, i.e. "<keyword>". So for example, if you had two posts titled My foo bar baz post and My foo BAZ before bar post respectively, then searching for "foo bar" would only match the first post which contains exactly foo followed by a space and bar, i.e. foo bar. But if you searched for foo bar (no quotes), then that would match both the posts because the title contains foo and also bar.

Also, the above "<keyword>" format is equivalent to setting the sentence argument to 1 or true as in s=my foo&sentence=1. But with the above format, you could further limit the search results, e.g. my foo&sentence=1 would match both the above posts, but "my foo", before would only match the second post because it contains before in addition to my foo.

However, it should be noted that both sentence and the "<keyword>" format will use the % sign in the LIKE clause, so for an exact match without that % sign, use the exact argument.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment