I have the problem that my client is using a theme which stores options of items in a serialized way in the post_meta table.

I already found out that this is not the best way to save values especially if you want to query over them later, but so far I cannot change that fact.

I tried to query it with the REGEXP compare option of the meta_query() function.

$metaquery['meta_query'] = array(
    'key' => '_ait-dir-item',
    'value' => '*\"guestcount\";s:[0-9]+:\"'.$_GET['guestcount'].'\".*',
    'compare' => 'REGEXP'

So I can find the items with the exact geustcount of the user's input.

But I would like to find all items with guestcount >= input.

How would that be possible?


I have tried it now with checking the number for a range with regex (while the regex is beeing created before)

$gc = (string)$_GET['guestcount']; //gc...guestcount
$input_regex = $gc[0];

for($i=1;$i<strlen($gc); $i++){
    $input_regex .= "[".$gc[$i]."-9]";
$compare_regex = '|[2-9][0-9]{2}|[1-9][0-9]{3}|1[0-9]{4}|20000';

the max value of the range is always 20.000.

Well ... I dont get any results with this... can anybody see a bug in it?

This was for the input = 150:

'value' => '*\"guestcount\";s:[0-9]+:\"\b(1|[5-9][0-9]{2}|[1-9][0-9]{3}|1[0-9]{4}|20000)\b\".*'


I cleaned up the regex and removed th \b and the . and * and stuff... now it seems, that I find alle items, where a guestcount is entered ... i.e. input=300 I also found one with 250 guestcount...

'value' => '\"guestcount\";s:[0-9]+:\"(1|[5-9][0-9]{2}|[1-9][0-9]{3}|1[0-9]{4}|20000)\"'

  • heureka. I solved it. The thing was, that the "constant" part, was not that constant. In the $compare regex, I also had to use the first $gc[0]+1 value (exception for 9). Shell I post it as an answer? – helle Dec 16 '14 at 10:07
  • 1
    Great, please add it as an answer ;-) .. and maybe explain how you make sure that the generated regex gives guestcount >= input. – birgire Dec 16 '14 at 10:13
  • Agree with @birgire post it as an answer. While I'm at it, I think that your answer is still relevant birgire. – Pieter Goosen Dec 16 '14 at 14:16
  • didnt have time so far ... but i will soon post it – helle Dec 22 '14 at 20:18

Your Answer

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

Browse other questions tagged or ask your own question.