0

I have a meta field called (sizes_avaliable) for my custom post type , and it looks like this :

sizes_avaliable for post id 1: 15-25-35-45

sizes_avaliable for post id 2: 55-65-75-85

and I have a search input that users can search their particular size in dropdown like this :

I need Size between:

11-19 ;

31-39 ;

51-59 ;

81-89 ;

So ,, if user searched for size from the drop-down ( 11-19 ) .. it will list the post id (1) because it has numbers (15) in the range ,

so I tried this query .

$meta_query = array(
    array(
        'key'     => 'sizes_avaliable',
        'value'   => '11-19',
        'compare' => '=',
    ),
);

but this doesn't show any results ..

can you help ??

thanks :)


update :

this is screenshot for my records : enter image description here

... so for example.. i need a user when he search 150-200

enter image description here

it will show the highlighted post ID because it has value 192

thanks. –

1
  • Post meta field values are saved in the database as strings. A query for a meta value of 11-19 will only return results if the meta value in the database is the exact string 11-19. You would have more success using a taxonomy to store size data because a taxonomy query allows you to query for an array of term IDs.
    – Andrew
    Commented Jan 7, 2018 at 14:31

1 Answer 1

2

you can use meta query operators like below query. See docs for meta_query parameters here

$range = $_GET['range']; // assuming you get the range from query string parameter which 11-19
$range_array = explode('-',$range);
$args = array(
    'post_type' => 'YOUR-POST-TYPE',
    'meta_query' => array( 
     array(
         'key' => 'sizes_avaliable',
         'value' =>  $range_array,
         'compare' => 'BETWEEN'
         ),
     )
); 

It would be better if you add screenshot of table containing record of sizes against id. so i will check for it if my answer doesn't work for you.

2
  • Noman thanks for your help ... and this is a screenshot for my records : this is screenshot for my records :[link]pasteboard.co/H1PZDbA.png ... so i need a user when he search 150-200 [link]pasteboard.co/H1Q2whk.png .. it will show the highlighted post ID because it has value 192 .. di you get it ?? thanks. Commented Jan 7, 2018 at 15:31
  • In my case I had to pass each range values in floatval() to make it work.
    – rAthus
    Commented Nov 13, 2018 at 16:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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