tax_query incompatible with numberposts?
-
Hi,
I found a problem with get_posts() when I use numberposts AND tax_query in the same query. When I use either indepently I don’t have the problem.
For example I need 6 random posts which have the given category or tag and excluding the current post.
$widget_posts_count = 6; $post_id = 39660; $widget_post_type = 'any'; $post_categories_list = array('8'); $term_id_list = array('6462'); $argsPosts = array( 'numberposts' => $widget_posts_count, 'exclude' => $post_id, 'orderby' => 'rand', 'post_type' => $widget_post_type, 'post_status' => 'publish', 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'category', 'field' => 'id', 'terms' => $post_categories_list ), array( 'taxonomy' => 'post_tag', 'field' => 'id', 'terms' => $term_id_list ) ) ); $postToDisplay = get_posts($argsPosts);
This is the returned query when I load the page (with both):
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) WHERE 1=1 AND wp_posts.ID NOT IN (39660) AND ( wp_term_relationships.term_taxonomy_id IN (8) OR tt1.term_taxonomy_id IN (6462) ) AND wp_posts.post_type IN (‘post’, ‘page’, ‘attachment’) AND (wp_posts.post_status = ‘publish’) GROUP BY wp_posts.ID ORDER BY RAND() DESC;
There’s no LIMIT here in returned query, but when I drop the tax_query, I have the LIMIT in the query :
SELECT wp_posts.ID FROM wp_posts WHERE 1=1 AND wp_posts.ID NOT IN (39660) AND wp_posts.post_type IN (‘post’, ‘page’, ‘attachment’) AND (wp_posts.post_status = ‘publish’) ORDER BY RAND() DESC LIMIT 0, 6
- The topic ‘tax_query incompatible with numberposts?’ is closed to new replies.