October 22, 2024
Chicago 12, Melborne City, USA
PHP

Compare two meta key values with between value


Records in CPT are as follow

Name Size From Size To
Demo 1 900 6000
Demo 2 5000 6000
Demo 3 7500 12000
Demo 4 2500 7500
Demo 5 9000 12000
$meta_query = array(
    'relation' => 'OR',
    array(
        array(
            'key' => '_size',
            'value'    =>  array(1000, 5000),
            'type'     => 'decimal(10, 2)',
            'compare'  => 'between'
        ),
        array(
            'key' => '_size_to',
            'value'    =>  array(1000, 5000),
            'type'     => 'decimal(10, 2)',
            'compare'  => 'between'
        ),
    ),
);

After applying above meta_query not able to get actual output

Expected Output

Name Size From Size To
Demo 1 900 6000
Demo 2 5000 6000
Demo 4 2500 7500

Actually it’s working like below SQL’s. Please review once and let me know best fit for this situation.

Right Now

SELECT * FROM test WHERE _size BETWEEN (1000 AND 5000) OR _size_to BETWEEN (1000 AND 5000)

Trying to do like below

SELECT * FROM test WHERE 1000 BETWEEN (_size AND _size_to) OR 5000 BETWEEN (_size AND _size_to)



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video