WP_Query meta_query where meta value ends in space

Note sure how your structure is, but here’s one way, using RLIKE comparison and a space character class:

array(
    'key'     => '_address_postcode',
    'value'   => '^SC1[[:space:]]',    // Starts with 'SC1 '  
    'compare' => 'RLIKE'
)

Maybe you should consider adjusting the meta values, as suggested by @cybmeta?

But note that meta queries can be slow, so alternatives might be better here (e.g. as a custom taxonomy?).

Leave a Comment