3

I'm running a standard WP_Query on a property website, and I want to find all properties that have a postcode beginning with 'SC1'. In the meta query, doing a normal LIKE on the postcode works, but it also returns properties with postcode SC13, which isn't what I want.

As a result, I've changed my meta query to be as follows:

array(
    'key'     => '_address_postcode',
    'value'   => 'SC1 ', // Notice the addition of the space
    'compare' => 'LIKE'
)

However.. it's not working. I've dug deeper and it seems that WordPress is trimming the values:

https://github.com/WordPress/WordPress/blob/af69f4ab1a0b44594b1f231c183f7a533575a893/wp-includes/class-wp-meta-query.php#L597

Any idea how I can search for meta values that specifically have a space at the end? I thought of doing something like so:

array(
    'key'     => '_address_postcode',
    'value'   => 'SC1 %',// Add a space then wildcard
    'compare' => 'LIKE'
)

... but that doesn't work. Can anyone think of a way to get around this :-S

  • 1
    Can I ask why do you have such kind of value and if the last space is really meaningful for the meta value? I don't think so. – cybmeta Nov 15 '16 at 18:19
  • Could you be a bit more clear? You say you want to return properties that start with sc1 but then it gives you sc1(3) which starts with sc1. What exactly are you looking for in your search? – rudtek Nov 15 '16 at 18:59
  • To be absolutely clear SC1 and SC13 are completely different postcodes in completely different areas. Imagine I have a page wehere I want to just show properties with a postcode that start with SC1. That's when I'd want to search postcodes beginning with 'SC1 ' (with space on end). I don't know how to make that any more clearer. – BIOSTALL Nov 15 '16 at 19:33
  • As far I undersntand, you don't need of the extra space in the query to do that. Anyway, you have a interesting answer. – cybmeta Nov 16 '16 at 6:22
3

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?).

  • Do you have evidence of this working? It's not working for me for some reason. Also, I can't find [:space:] mentioned anywhere on the web. Any links to resources would be great. – BIOSTALL Nov 15 '16 at 19:37
  • 1
    I use MariaDB, so I linked to their KB. There was also a typo that I fixed . @BIOSTALL – birgire Nov 15 '16 at 19:44
-1

You can use   in your meta query.

array(
    'key'     => '_address_postcode',
    'value'   => 'SC1 ',
    'compare' => 'RLIKE'
)
  • Do you have evidence of this working? It's not working for me, and surely that would search for the actual string ' '? – BIOSTALL Nov 15 '16 at 19:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.