我试图查询特定年龄段的帖子。例如,我有一个年龄在5-10岁之间的帖子。如果我使用以下代码,我会在搜索年龄>=5岁和10岁时得到一个结果:
$meta_query = array(
\'key\' => \'_age_from\',
\'value\' => sanitize_text_field( $_REQUEST[\'age_from\'] ),
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
);
$meta_query = array(
\'key\' => \'_age_to\',
\'value\' => sanitize_text_field( $_REQUEST[\'age_to\'] ),
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
);
但我也希望在搜索6-9岁的孩子时得到结果。我认为这个SQL查询应该可以做到这一点,但我目前无法在wordpress中查询:
WHERE (6 BETWEEN _age_from AND _age_to) AND (9 BETWEEN _age_from AND _age_to)
有谁能帮我解决这个问题吗?太棒了!
SO网友:Jignesh Patel
你可以这样写。
\'meta_query\' => array(
array(
\'key\' => \'_age_from\',
\'value\' => sanitize_text_field( $_REQUEST[\'age_from\'] ),
\'compare\' => \'>=\',
\'type\' => \'NUMERIC\'
),
array(
\'key\' => \'_age_to\',
\'value\' => sanitize_text_field( $_REQUEST[\'age_to\'] ),
\'compare\' => \'<=\',
\'type\' => \'NUMERIC\'
)
);