• I have property plugin with two fields in the backend from Area and To Area.
    In the front end search also I have included two search fields min area and max So if either the min area or max area falls in range of property from area or to area then it should show up result..

    Eg one of the properties has area between 1000-3000 as from area and to area and anyone in the front end searches either 500-2000 or 1500 -6000 shld show a result ,since in the first case 2000 falls in 1000-3000 range and in second case 1500 falls in 1000-3000 range.
    I dont know whats the exact way to write a meta query comparing a value between two keys with an OR condition this is what I have achived, I get some results using <= or >= but dont know how to use between if possible please please write the code.
    `$meta_query = array(
    ‘relation’ => ‘OR’,
    array(

    ‘key’ => array(‘property_frmarea’,’property_toarea’) (these are the 2 backend fields)
    ‘value’ => $_POST[‘from_size’], (this is front end from field)
    ‘type’ => ‘numeric’,
    ‘compare’ => ‘Between’
    ),
    array(
    ‘key’ => array(‘property_frmarea’,’property_toarea’)
    ‘value’ => $_POST[‘to_size’],
    ‘type’ => ‘numeric’,
    ‘compare’ => ‘Between’
    ),
    );

Viewing 3 replies - 1 through 3 (of 3 total)
  • Anur,

    I think you have to approach it from an opposite perspective. Try the following example:


    <?php
    $meta_query = array(
    'relation' => 'OR',
    array(
    'key' => 'property_frmarea',
    'value' => array(
    intval( $_POST['from_size'] ), // don't forget to escape user input
    intval( $_POST['to_size'] )
    ),
    'type' => 'UNSIGNED',
    'compare' => 'BETWEEN'
    ),
    array(
    'key' => 'property_toarea',
    'value' => array(
    intval( $_POST['from_size'] ), // don't forget to escape user input
    intval( $_POST['to_size'] )
    ),
    'type' => 'UNSIGNED',
    'compare' => 'BETWEEN'
    ),
    );

    Thread Starter anur

    (@anur)

    Yes that does give output but it works the opposite way…
    Its like this in the backend if a property has
    from size : 400 and to size :1000

    So anyone who searches in min size or max size in that range will show a result.
    So some one searches for 600 -1200 will show a result since 600 falls in 400-1000 range
    Similarly some one searches 100-800 will show a result since 800 falls in 400-1000 range.

    The solution you gave me works the opposite way it searches key betwen two values but I want the value to searcb between two keys.
    Hope some one can please help…

    The only way to do that would be to construct your own custom SQL query.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘wordpress value compare between two meta keys’ is closed to new replies.