Question

Everything else in these args works great. The only thing that isn't working is the exclusion of the uncategorized category. I've tried both 'cat' => '-1', where 1 is the ID of the uncategorized category I want to exclude, and also a tax_query like I have below—the part in question being:

array(
    'taxonomy' => 'category',
    'field'    => 'slug',
    'terms'    => 'uncategorized',
    'operator' => 'NOT IN',
),

Here's the full code:

$tourDateArtists = get_terms( 
    array(
        'taxonomy'   => 'category',
        'hide_empty' => false,
    )
);
$today = strtotime('today');

foreach ( $tourDateArtists as $tourDateArtist ) {                       
    $tourDateArtist_query = new WP_Query( array(
        'post_type' => 'tour-date',
        // 'posts_per_page' => -1,
        'meta_key'     => 'wpcf-tour-date',
        'orderby'      => 'meta_value_num',
        'order'        => 'ASC',
        'tax_query' => array(
            'relation' => 'AND',
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => array( $tourDateArtist->slug ),
                'operator' => 'IN',
            ),
            array(
                'taxonomy' => 'category',
                'field'    => 'slug',
                'terms'    => 'uncategorized',
                'operator' => 'NOT IN',
            ),
        ),
        'meta_query' => array(
            array(
                'key'     => 'wpcf-tour-date',
                'value'   => $today,
                'compare' => '>=',
                'type'    => 'NUMERIC',
            ),
        ),
    ));

    if ( $tourDateArtist_query->have_posts() ) {
        echo '<div id="' . $tourDateArtist->slug . '" class="has-dates">';
        echo '<h2><a href="/artists/' . $tourDateArtist->slug . '">' . $tourDateArtist->name . '</a></h2>';
        echo '<ul class="' . $tourDateArtist->slug . '-tour-dates">';
        while ( $tourDateArtist_query->have_posts() ) {
            $tourDateArtist_query->the_post();
            echo '<li class="tour-date-item">';
            echo '<a href="' . types_render_field( 'tour-date-link' ) . '">';
            echo '<div class="tour-date-date">' . types_render_field( 'tour-date', array( 'format' => 'F j, Y g:i a') ) . '</div>';
            echo '<div class="tour-date-city-state">' . types_render_field( 'city-state' ) . '</div>';
            echo '<div class="tour-date-venue">' . get_the_title() . '</div>';
            echo '</a>';
            echo '</li>';
        }
        echo '</ul>';
        echo '</div>';

        // echo ('<a href="/tour-dates#' . $parentSlug . '" class="load-more-link">Show More Dates</a>');

    } else {
        /* show 'no news' message if no tour dates */
        echo '<div id="' . $tourDateArtist->slug . '" class="no-dates">';
        echo '<h2>' . $tourDateArtist->name . '</h2>';
        echo '<p>' . $tourDateArtist->name . ' is not on tour.' . '</p>';
        echo '</div>';
    }
    $tourDateArtist_query = null;
    wp_reset_postdata();
}

It's a foreach but it should test each time against that tax_query right? Any idea why it wouldn't be excluding the category I have defined: uncategorized?

Was it helpful?

Solution

After a lot of searching I ended up finding this option that worked: Adding one exclude parameter to the get_terms list at the top. Like this:

$tourDateArtists = get_terms( 
    array(
        'taxonomy'   => 'category',
        'hide_empty' => false,
        'exclude'    => '1',
    )
);

Still not sure why neither 'cat' => '-1', nor the particular tax_query param I was using were working, but the above does, so it'll do.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top