Your SQL command is highly insecure and open to security issues like SQL injection, so even if this may not answer the question, I strongly suggest you to use $wpdb->prepare()
and $wpdb->esc_like()
— the latter is used to escape the %
character in SQL.
Additionally, you can simply use $wpdb->posts
to output the table name for WordPress posts such as wp_posts
.
And I noticed that in your SQL command:
-
The table name is incorrect because the
FROM
and$wpdb->prefix
is concatenated as one word likeFROMwp_posts
. -
There’s a whitespace after the second
%
in theLIKE
clause:%". $_POST['value']. "% '
— so that whitespace is probably not needed? Or that it could be the reason why the query did not return any results. -
The
var_dump()
actually contains no accent — you useddefinition
and notdéfinition
. Same goes with the direct one.
Now here’s how your query or SQL command should be generated:
$value = $_POST['value'] ?? '';
// wrapped for brevity
$sql = $wpdb->prepare( "
SELECT DISTINCT *
FROM {$wpdb->posts}
WHERE post_title LIKE %s
", '%' . $wpdb->esc_like( $value ) . '%' );
$res = $wpdb->get_results( $sql );
And I actually tested the above code with a post with the title containing the word définition
, and the query returned one result (which is a test post).
If my code doesn’t work for you, you can try sanitize_text_field()
, but that will strip HTML tags, among other things.