I need to select posts who are in multiple categories and have certain post metavalue. I can select multiple categories with
query_posts( array( 'category__and' => array(1,2,3) ) );
But also I need from those selected categories, select only those posts who have certain meta_value from wp_postmeta table. This task I can do with sql query:
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
I don’t understand how can I select for example only those posts who has category 1 AND 2 AND 3 AND postmeta meta_key length is BETWEEN $min_length AND $max_length?
Since version 3.1 WP got much enhanced querying of custom fields. If I got the code right your query should be something like this:
See: