When I call WP_Query with tax query or category__in, I get posts only from first category in array of requested categories.
$query = new WP_Query(
array(
"tax_query" => array(
array(
"taxonomy" => "category",
"field" => "term_id",
"terms" => array(1, 2, 3)
)
),
"posts_per_page" => -1
)
);
Here is sql query from $query->request:
SELECT wp_posts.*
FROM wp_posts
INNER JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id)
INNER JOIN wp_term_relationships AS tt1 ON (wp_posts.ID = tt1.object_id)
INNER JOIN wp_term_relationships AS tt2 ON (wp_posts.ID = tt2.object_id)
WHERE 1=1
AND (wp_term_relationships.term_taxonomy_id IN (1,
2,
3)
AND tt1.term_taxonomy_id IN (1)
AND tt2.term_taxonomy_id IN (1))
AND wp_posts.post_type="post"
AND (wp_posts.post_status="publish"
OR wp_posts.post_status="future"
OR wp_posts.post_status="draft"
OR wp_posts.post_status="pending"
OR wp_posts.post_status="private")
GROUP BY wp_posts.ID
ORDER BY wp_posts.post_date DESC
Results list only posts in category 1.
What is wrong with my query?
1 Answer
Found problem. I replaced:
$posts = $query->get_posts();
with:
$posts = array();
if ($query->have_posts()){
while ($query->have_posts()){
$query->next_post();
array_push($posts, $query->post);
}
}
And everything is working now.