WordPress.org

Ready to get started?Download WordPress

Forums

Using Multiple Taxonomy Query in WP_Query (4 posts)

  1. Aikyrie
    Member
    Posted 8 months ago #

    I followed the instructions here http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters 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:

    <?php
    	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',
    			array(
    				'taxonomy' => 'related-services',
    				'field' => 'id',
    				'terms' => array($services)
    			),
    			array(
    				'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'); ?>
                </div>
            </div>
        <?php endwhile; endif;
    	wp_reset_query(); ?>

    Any help would be greatly appreciated. Thank you!

  2. bcworkz
    Member
    Posted 8 months ago #

    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.

  3. Aikyrie
    Member
    Posted 8 months ago #

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

  4. bcworkz
    Member
    Posted 8 months ago #

    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.

Reply

You must log in to post.

About this Topic