Support » Developing with WordPress » Filter/multi-taxonomy query

  • mfebrey

    (@mfebrey)


    Hello!

    I have some Custom Post Types with custom taxonomies. I’m trying to build a query for two of the taxonomies… to filter Location (tax) results by Job Title (tax). I am at a loss why I can’t get the basic query to even begin. Do you see what’s wrong??

    Thanks in advance!

    
     <?php 
    
    	$args = array(
    		'relation' => 'AND',
    		array(
    			'taxonomy' => 'location',
    			'field'    => 'slug',
    			'terms'    => 'Denver',
    		),
    		array(
    			'taxonomy' => 'job_title',
    			'field'    => 'slug',
    			'terms'    => 'photographer',
    		),
    	);
    
        echo "line after args declaration....<br>";
        
        
        
    	// Custom query.
        $query = new WP_Query( $args );
        
        echo "line after query declaration....<br>";
        
        
         
        // Check that we have query results.
        if ( $query-> have_posts() ) {
         
            // Start looping over the query results.
            while ( $query->have_posts() ) {
         
                $query->the_post();
         
                echo "Success <br>";
         
            }
         
        } else {
            echo "FAIL <br>";
        };
        
        echo "End of line";
         
        // Restore original post data.
       wp_reset_postdata();
     
    ?>
    

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Take a look at documentation for taxonomy parameters in WP_Query:

    https://developer.wordpress.org/reference/classes/wp_query/#taxonomy-parameters

    For example displaying posts from several taxonomies:

    
    $args = array(
        'post_type' => 'post',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'movie_genre',
                'field'    => 'slug',
                'terms'    => array( 'action', 'comedy' ),
            ),
            array(
                'taxonomy' => 'actor',
                'field'    => 'term_id',
                'terms'    => array( 103, 115, 206 ),
                'operator' => 'NOT IN',
            ),
        ),
    );
    $query = new WP_Query( $args );
    

    Looking at your code example, it does not seem to use tax_query parameter.

Viewing 1 replies (of 1 total)
  • The topic ‘Filter/multi-taxonomy query’ is closed to new replies.