Here are few ideas:
1) You could try to compare with LIKE
instead:
'key' => '_my_custom_field_key',
'value' => 'test1',
'compare' => 'LIKE'
It should give you a SQL query with:
... LIKE '%test1%' ...
2) You might want to save the string as
,test1,test2,test3,
to make the whole word search easier:
'key' => '_my_custom_field_key',
'value' => ',test1,',
'compare' => 'LIKE'
3) Another option would be to save it to a meta key with multiple values.
4) You could also modify the generated SQL query via filters like posts_where
and posts_clauses
. Notice that get_posts()
suppresses filters by default, but you can use the suppress_filters
parameter to control it, or use WP_Query()
instead.
5) Manually write the SQL query and fetch it with the $wpdb
class, like you are doing already.