• Hi, I stumbled upon a really weird bug(?). I have follwowing query:

    $myQuery = new WP_Query(array(
    	'post_type' => 'custom_type',
    	'posts_per_page' => -1,
    	'tax_query' => array(array(
    		'taxonomy' => 'custom_taxonomy',
    		'field' => 'id',
    		'terms' => array(282),
    		'operator' => 'NOT IN'
    	))
    ));

    What I am expecting is to get only posts that don’t have the custom_taxonomy term with the ID 282. But this query also leaves out some posts that don’t have the term with ID 282. So for example a post with following wp_get_post_terms result:

    Array
    (
        [0] => stdClass Object
            (
                [term_id] => 32
                [name] => Custom term
                [slug] => custom_term
                [term_group] => 0
                [term_order] => 0
                [term_taxonomy_id] => 36
                [taxonomy] => custom_taxonomy
                [description] =>
                [parent] => 0
                [count] => 3
                [filter] => raw
            )
    )

    As you can see, this post has no term with the ID 282. The query doesn’t show none of the 3 posts (see [count] => 3) when I say NOT IN 282.

    Did I just miss something in the query? It just doesn’t make sense for me :/

    Many thanks for your help!

    PS: I had some plugins running but deactivated them for bugfinding purposes, didn’t fix the problem.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Samuel Wood (Otto)

    (@otto42)

    WordPress.org Admin

    Dump the $myQuery. Somewhere in there will be the SQL it created to get the posts. Examine that to see what the issue might be.

    Thread Starter Gerald

    (@painsicook)

    Sorry for the late reply. This is a very useful tip to show what’s happening under the hood 🙂 Thanks for that! The SQL from the query above is following:

    SELECT wp_posts.*
    FROM wp_posts
    JOIN wp_icl_translations t ON wp_posts.ID = t.element_id AND t.element_type = 'post_custom_type'
    JOIN wp_icl_languages l ON t.language_code=l.code AND l.active=1 WHERE 1=1  AND ( wp_posts.ID NOT IN (
    	SELECT object_id
    		FROM wp_term_relationships
    		WHERE term_taxonomy_id IN (36,289)
    	)
    ) AND wp_posts.post_type = 'game' AND (wp_posts.post_status = 'publish') AND t.language_code='en'
    GROUP BY wp_posts.ID
    ORDER BY wp_posts.post_title ASC

    Shouldn’t be the 282 term ID used somewhere?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘tax_query filters more than it should’ is closed to new replies.