I am trying to create a search filter using meta_query. The only problem I’m having now is that I can’t compare with wildcards. I think that’s how you word it?
$keyword = 'sos';
$tlds = array('com','net','org');
query_posts( array(
'post_type' => 'domains',
'meta_query' => array(
array('key' => 'domain_name',
'value' => $keyword,
'compare' => 'LIKE'
),
array('key' => 'domain_tld',
'value' => $tlds,
'compare' => 'IN'
)
)
));
This code will work fine, but right now it just checks whether the domain_name contains the keyword anywhere in the string. I want to check whether the domain_name begins, contains or ends with the keyword.
I tried adding the ‘%’ sign to define the wildcard so that it would search for the pattern, but I don’t think meta_query allows wildcards.
$keyword = 'sos%';
function where_keyword( $where = '' ){
$where .= " LEFT JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
WHERE $wpdb->postmeta.meta_key = 'domain_name'
AND $wpdb->postmeta.meta_value LIKE '$keyword'";
return $where;
}
add_filter( 'posts_where' , 'where_keyword');
This was my second attempt, but I am not sure if my approach is correct? I’m a novice when it comes to sql.
I’ve been get this working for days so I thought I’d see if anyone else has attempted this.