0

I have a simple query. Getting the posts only if it have both the taxonomies in it.

$args = array(
            'post_type' => 'treatment',
            'tax_query' => array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'body-parts',
                    'field' => 'term_taxonomy_id',
                    'terms'    => $_SESSION["body-parts"],
                ),
                array(
                    'taxonomy' => 'conditions',
                    'field' => 'term_taxonomy_id',
                    'terms'    => $_SESSION["conditions"],
                ),
            ),
            'posts_per_page' => -1,
            'fields' => 'ids',
    );
$treatments = new WP_Query($args);
$treatment_ids_array = $treatments->posts;
var_dump($treatment_ids_array);

Now this array should be empty when there is no treatment (custom post type) related with both the taxonomies - instead it is showing all post ids in this array. Otherwise everything is working correctly - when there is actually a post with both the taxonomies - then only that post is showing.

9
  • "instead it is showing all post ids" - try var_dump( $args, $treatments->request ); and share the output, and you could also temporarily suppress all filters by adding 'suppress_filters' => true to your args.
    – Sally CJ
    Jul 18 at 7:15
  • @SallyCJ CJ I did the var_dump as requested. Also printed the sessions as well. Conditions: 102 Body Parts: 29 array(4) { ["post_type"]=> string(9) "treatment" ["tax_query"]=> array(3) { ["relation"]=> string(3) "AND" [0]=> array(3) { ["taxonomy"]=> string(10) "body-parts" ["field"]=> string(16) "term_taxonomy_id" ["terms"]=> string(2) "29" } [1]=> array(3) { ["taxonomy"]=> string(10) "conditions" ["field"]=> string(16) "term_taxonomy_id" ["terms"]=> string(3) "102" } } ["posts_per_page"]=> int(-1) ["fields"]=> string(3) "ids" }
    – Joy
    Jul 20 at 7:58
  • @Sally 1 comment was too long string(564) "SELECT mod284_posts.ID FROM mod284_posts LEFT JOIN mod284_term_relationships ON (mod284_posts.ID = mod284_term_relationships.object_id) LEFT JOIN mod284_term_relationships AS tt1 ON (mod284_posts.ID = tt1.object_id) WHERE 1=1 AND ( mod284_term_relationships.term_taxonomy_id IN (29) AND tt1.term_taxonomy_id IN (102) ) AND mod284_posts.post_type = 'treatment' AND (mod284_posts.post_status = 'publish' OR mod284_posts.post_status = 'acf-disabled' OR mod284_posts.post_status = 'private') GROUP BY mod284_posts.ID ORDER BY mod284_posts.post_date DESC "
    – Joy
    Jul 20 at 7:59
  • The query looks good to me. So with that query, were there supposed to be any results? Or should the query yield an empty array and yet it actually contained IDs of your treatment posts? 🤔 Have you tried using static values like 'terms' => 29, and did the same issue persist? How about suppressing all filters - have you tried that? And are you sure this is not a caching issue?
    – Sally CJ
    Jul 20 at 10:39
  • Test 1: 102 + 29 = 0 - There is no post for these terms AND. It shows all posts. Test 2: 96 + 9 = 197 - post ID is 197 which have both terms with id 96 AND 9.
    – Joy
    Jul 20 at 11:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.