WordPress.org

Forums

wp_query exclude taxonomy (3 posts)

  1. hp3
    Member
    Posted 4 years ago #

    I want to show posts from a custom post type that have a term from one taxonomy but exclude posts that have a term from another taxonomy.

    show all posts from a given post type that have a term named 'A' with a value of 'foo' from a taxonomy named 'taxonomy 1' but exclude any posts which also have a term named 'B' with a value of 'bar' from a taxonomy named 'taxonomy 2'

    can I perform this type of query by passing an array to wp_query or do I have to write a SELECT statement and query the database directly?

    The taxonomy query examples on the wp_query use 'relation' such as "AND" and 'operator' such as "NOT IN" to refine a query but it is not clear to me how to use these in an array to build my query for wp_query.

  2. web559
    Member
    Posted 4 years ago #

    Bump.

  3. Creative Slice
    Member
    Posted 3 years ago #

    Here's how to exclude a custom taxonomy but show the rest of the posts from this "news" custom post type:

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

Topic Closed

This topic has been closed to new replies.

About this Topic