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)

Comment (1)

Jese Leos

18 hours ago

Verified user

Here I compared below conditions, The value of the _size field must be less than or equal to 5000. The value of the _size_to field must be greater than or equal to 1000. When we filter like Min[1000] & Max[12000] _size <= 1000 _size_to >= 5000 <?php $filter_size_rang_min_val = 1000; $filter_size_rang_max_val = 5000; $meta_query = array( 'relation' => 'AND', array( 'key' => '_size', 'value' => $filter_size_rang_max_val, 'type' => 'NUMERIC', 'compare' => '<=' ), array( 'key' => '_size_to', 'value' => $filter_size_rang_min_val, 'type' => 'NUMERIC', 'compare' => '>=' ), );

You’ll be in good company