• I’m trying to query some custom post types based on terms from a custom taxonomy. The arguments I’m putting into WP_Query are:

    Array
    (
        [post_type] => Array
            (
                [0] => event
            )
    
        [post_status] => publish
        [posts_per_page] => -1
        [orderby] => title
        [order] => ASC
        [tax_query] => Array
            (
                [0] => Array
                    (
                        [taxonomy] => event-type
                        [field] => slug
                        [operator] => IN
                        [terms] => Array
                            (
                                [0] => walk
                                [1] => talk
                                [2] => workshop
                            )
    
                    )
    
            )
    
    )

    But all I’m getting back are posts with the ‘walk’ term. If I change the order of the tax_query array I find that it always appears to be the first term in the array.

    The SQL this translates into is:

    SELECT wp_posts.* 
    FROM wp_posts  
    LEFT JOIN wp_term_relationships ON (wp_posts.ID = wp_term_relationships.object_id) WHERE 1=1  
    AND ( wp_term_relationships.term_taxonomy_id IN (32,33,34)) 
    AND wp_posts.post_type = 'event' 
    AND ((wp_posts.post_status = 'publish')) 
    GROUP BY wp_posts.ID 
    ORDER BY wp_posts.post_title ASC

    I can’t see what’s wrong with this code but it’s been bugging me for a while now.

    The argument array is actually built using shortcode attributes, but the above code is what is generated and passed to WP_Query.

    Any idea what’s going on here?

    thanks

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter chrishuh

    (@chrishuh)

    Doing a little more testing and this works:

    $args = array(
        'post_type' => 'event',
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'orderby' => 'title',
        'order' => 'ASC',
        'tax_query' => array(
            'relation' => 'OR',
    		array(
    			'taxonomy' => 'event-type',
    			'field'    => 'slug',
    			'terms'    => 'walk'
    		),
    		array(
    			'taxonomy' => 'event-type',
    			'field'    => 'slug',
    			'terms'    => 'talk'
    		),
    		array(
    			'taxonomy' => 'event-type',
    			'field'    => 'slug',
    			'terms'    => 'workshop'
    		)
        ),
    );

    It’s less tidy but works. So it seems to be something to do with passing multiple taxonomy terms as an array in a single tax_query.

    Moderator bcworkz

    (@bcworkz)

    It’s not multiple terms, at least not on my site. Query args like in your OP work fine for me when terms applicable to my site are used. Same goes for your SQL when used in $wpdb->get_results(). It seems there may be a theme or plugin conflict at play somewhere.

    Anyway, I’m glad you found something that works. I thought you might like to know there is nothing wrong with your original query when used in a plain default WP site. You are not going crazy 🙂

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘WP_Query only returns posts that match first term in tax_query array’ is closed to new replies.