Support » Plugins » Hacks » Using Multiple Taxonomy Query in WP_Query

  • I followed the instructions here in regards to using multiple custom taxonomies but my code doesn’t seem to be working and I can’t figure out why.

    I am tagging the page with specific taxonomies so that they’ll display a specific page when they are tagged with those taxonomies. For example, if I tag page A with page 1, page 1 will display as a related page.

    It’s returning the right terms but the query isn’t spitting out anything.

    Here’s my code:

    	global $post;
    	$services_terms = get_the_terms( $post->ID, 'related-services' );
    	if ( $services_terms && ! is_wp_error( $terms ) ) :
    	$services_name = array();
    	foreach ( $services_terms as $services_term ) {
    		$services_id[] = $services_term->term_taxonomy_id;
    	$services = join( ", ", $services_id );
    	$related_service = new WP_Query(array(
    		'posts_per_page' => '4',
    		'post_type' => 'page',
    		'tax_query' => array(
    			'relation' => 'AND',
    				'taxonomy' => 'related-services',
    				'field' => 'id',
    				'terms' => array($services)
    				'taxonomy' => 'related-projects',
    				'field' => 'id',
    				'terms' => '730',
    				'operator' => 'NOT IN'
    	while($related_service->have_posts()) : $related_service->the_post(); ?>
        	<div id="post-<?php the_ID(); ?>" <?php post_class('serv-block'); ?>>
            	<div class="serv-title-wrap">
                	<div class="serv-title"><a href="<?php the_permalink(); ?>" alt="<?php the_title(); ?>"><?php the_title(); ?></a></div>
    				<?php the_post_thumbnail('pro-serv-thumb'); ?>
        <?php endwhile; endif;
    	wp_reset_query(); ?>

    Any help would be greatly appreciated. Thank you!

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz


    The only thing I see that might be wrong is the line 'terms' => array($services). This argument accepts either a string or an array, but I suspect it’s confused by an array of a single comma delimited string of IDs. Either simply pass $services_id array directly (better) or pass $services without putting it in an array (will work too).

    Not sure this is really a problem, but worth a try since I see nothing else wrong.

    It was a no go. Thank you for trying though!

    Moderator bcworkz


    Bummer! You could try hooking the ‘posts_request’ filter and echoing out the passed SQL query. I have found examining the actual query formed by a new WP_Query object is a good clue as to what’s wrong with the WP_Query arguments. There’s often some mutually exclusive conditions that prevent the query from ever returning anything.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Using Multiple Taxonomy Query in WP_Query’ is closed to new replies.