WordPress.org

Ready to get started?Download WordPress

Forums

Exclude custom taxonomy from wp_query (4 posts)

  1. imgraeme
    Member
    Posted 3 years ago #

    I am trying to figure out how to exclude a custom taxonomy from a wp_query I am running. I have set up a custom post type and registered a new taxonomy and with no trouble at all I can include the taxonomy in the query array with 'taxonomy' => 'slug' but instead of including I want to exclude a single slug from the query.

    I have tried solutions such as post__not_in, cat=-ID, etc. with no luck.

    I have yet to find a proper solution on the codex, forum, or anywhere else for that matter. This may be something simple and I need sleep, but any help would be amazing!

    Here is the whole query and array. Here 'projecttype' is the custom taxonomy as part of the custom portfolio post type. Instead of including the 'featured' project type I want to exclude it.
    new WP_Query(array('projecttype' => 'featured', 'post_type' => 'portfolio', 'posts_per_page' => 1));

  2. MichaelH
    Member
    Posted 3 years ago #

    Don't know that you are going to get there in 3.0.1 but 3.1 may support that (see http://core.trac.wordpress.org/ticket/12891 for more info on that).

    In the meantime you either have to resort to using wpdb or retrieve all the posts in that post type then filter out that taxonomy in your loop.

    There is this plugin but don't know that it will help http://wordpress.org/extend/plugins/query-multiple-taxonomies/

  3. imgraeme
    Member
    Posted 3 years ago #

    Thanks Michael. Any idea why cat=-ID wouldn't be working? Same with post__not_in, tag__not_in, etc.

    Another note is when I go into the custom taxonomy page in the admin to get the ID of each of the different categories, they show up as tag_ID=29 rather then cat_ID=29.

    I can include all custom taxonomies in the query, how can I then exclude one from the loop?

  4. MichaelH
    Member
    Posted 3 years ago #

    post__not_in, category__not_in, and tag__not_in should work.

    That's just the way they show ids

    This will determine if there are any terms in a taxonmy for a given post/custom post and display the terms else it will do nothing...just an example you can fit to your need

    $taxonomy='your_taxonomy';
      $object_terms = wp_get_object_terms($post->ID, $taxonomy, array('fields' => 'all'));
      if ($object_terms) {
        echo 'terms for '.$taxonomy;
        foreach ($object_terms as $term) {
          echo '<p><a href="' . esc_attr(get_term_link($term, $taxonomy)) . '" title="' . sprintf( __( "View all posts in %s" ), $term->name ) . '" ' . '>' . $term->name.'</a><p> ';
        }
      } else {
    //this taxonomy doesn't exist for this post
    }

Topic Closed

This topic has been closed to new replies.

About this Topic