Custom Taxonomy Filtering with WordPress

Ive been trying to figure this out for three days now, even using solutions on this site. I still cant get this working.

I have a wordpress loop that uses a filter to show posts by post type. Now the post type is called “case-studies” Thus all the posts in the type case studies are shown.

Read More

But i need to hide a specific taxonomy term from this loop. The taxonomy is called “sectors” and the term is “healthcare”. Ive tried all manner of combinations but still cant get this. I need this pretty urgent. Anyone who can help would save my life.

Here is the query and the loop

<?php
// The Query
$the_query = new WP_Query( 'post_type=case-studies&posts_per_page=-1' );

// The Loop
while ( $the_query->have_posts() ) :
    $the_query->the_post(); 


?>

Related posts

Leave a Reply

3 comments

  1.     $args = array(
        'post_type' => 'case-studies',
        'tax_query' => array(
            array(
                'taxonomy' => 'sectors',
                'field' => 'slug',
                'terms' => array('comercial', 'personal', 'etc') //excluding the term you dont want.
            )
        )
    );
    $query = new WP_Query( $args );
    

    I dind´t try it but you could just make a query calling only the terms you want, you could previously populate the terms array listing all the terms on the taxonomy and excluding the one you want, i think this is a little hacky it should be another straight forward way to do it but give it a try since it is a case of life or dead =).

    source: http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

  2. Try this one:

    <?php
    $type = 'cpreviews';
    $args=array(
      'post_type' => $type,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1
    );
    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <p><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></p>
        <?php
      endwhile;
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    
  3. ok then try this one it will extract your terms dinamicaly and exclude the term you dont want, i didn´t check if it is working but this is the logic, please check for sintax errors.

    $terms = get_terms("sectors");
    $count = count($terms);
    $termsAr = array();
    if ($count > 0 ){
        foreach ( $terms as $term ) {
            if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
                array_push($termsAr, $term->name);
            }
        }
    }
    
    $terms = get_terms("types");
    $count = count($terms);
    $termsAr2 = array();
    if ($count > 0 ){
        foreach ( $terms as $term ) {
            if($term->name !== "healthcare"){//Here we exclude the term or terms we dont want to show
                array_push($termsAr2, $term->name);
            }
        }
    }
     $args = array(
        'post_type' => 'case-studies',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'sectors',
                'field' => 'slug',
                'terms' => $termsAr //excluding the term you dont want.
            ),
            array(
                'taxonomy' => 'types',
                'field' => 'slug',
                'terms' => $termsAr2 //excluding the term you dont want.
            )
        )
    );
    $query = new WP_Query( $args );