如何从多个类别和元值中选择帖子?

时间:2011-04-04 作者:phpdew

我需要选择谁是在多个类别的职位,并有一定的职位元价值。我可以使用选择多个类别

query_posts( array( \'category__and\' => array(1,2,3) ) );
但我还需要从这些选定的类别中,仅从wp\\U Posteta表中选择那些具有特定meta\\u值的帖子。我可以使用sql查询执行以下任务:

SELECT DISTINCT (
p.ID
), p . *
FROM wp_posts AS p
INNER JOIN wp_postmeta AS p1 ON ( p.ID = p1.post_id )
CROSS JOIN wp_postmeta AS p3
USING ( post_id )
WHERE p3.meta_key = \'length\'
AND convert( p3.meta_value, signed )
BETWEEN \'".$min_length."\'
AND \'".$max_length."\'
AND post_status = \'publish\'
$subSelect
ORDER BY p.ID DESC
我不明白如何才能只选择那些类别为1、2和3且Posteta meta\\u密钥长度介于$min\\u length和$max\\u length之间的帖子?

1 个回复
SO网友:Rarst

自3.1版以来,WP对自定义字段的查询得到了很大的增强。如果代码正确,您的查询应该是这样的:

query_posts(array(
    \'category__and\' => array(1,2,3),
    \'meta_query\' => array(
        array(
            \'key\' => \'length\',
            \'value\' => array($min_length, $max_length),
            \'compare\' => \'BETWEEN\',
            \'type\' => \'NUMERIC\',
        ),
    ),
));
请参见:

结束