• Hello –

    I inherited a project and I’ve been trying to figure out how to exclude a custom taxonomy from a loop and haven’t had much success.

    I thought it would be pretty straightforward and also found this link on SO which deals with the same issue and suggests the following code:

    $happening = new WP_Query(
    array(
      'post_type'  => 'news',        // only query News post type
      'tax_query' => array(
        array(
            'taxonomy'  => 'news-cat',
            'field'     => 'slug',
            'terms'     => 'media', // exclude items media items in the news-cat custom taxonomy
            'operator'  => 'NOT IN')
    
            ),
       )
    );

    I adapted this to the page I’m working on but it doesn’t seem to want to filter out the “camp” taxonomy that I’m hoping to filter. It’s still showing up in the loop.

    Here’s what I’ve done.

    <?php
    
    			$news_category= "";
    
    			if(isset($_GET['category'])){
    				$news_category = $_GET['category'];
    			}
    
    			//Checking for pagination
                $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
    
    			if($news_category){
    				// args
    				$args = array(
    					'posts_per_page'	=> 10,
    					'post_type'		=> array('news', 'post'),
    					'paged' => $paged,
    					'tax_query' => array(
    							array(
    								'taxonomy' => 'news_category',
    								'field'    => 'slug',
    								'terms'    => 'camp',
    								'operator' => 'NOT IN'
    							),
    					 ),
    				);
    			}else{
    				// args
    				$args = array(
    					'posts_per_page'	=> 10,
    					'post_type'		=> array('news','post'),
    					'paged'              => $paged,
    				);
    			}
    			// query
    			$the_query = new WP_Query( $args );
    
    		?>

    Am I missing something obvious here?

    Thanks!

  • The topic ‘Excluding Custom Taxonomy from Loop’ is closed to new replies.