Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to exclude photo and video categories (ID's 1 and 2 respectively) from my custom taxonomy page.

Any suggestions on how can I exclude these two categories from the loop?

<?php
/**
 * Locations taxonomy archive
 */
get_header();
$term = get_term_by( 'slug', get_query_var( 'term' ), get_query_var( 'taxonomy' ) );
?>
<div class="wrapper">
    <div class="primary-content">
        <h1 class="archive-title"><?php echo apply_filters( 'the_title', $term->name ); ?> News</h1>

        <?php if ( !empty( $term->description ) ): ?>
        <div class="archive-description">
            <?php echo esc_html($term->description); ?>
        </div>
        <?php endif; ?>

        <?php if ( have_posts() ): while ( have_posts() ): the_post(); ?>

        <div id="post-<?php the_ID(); ?>" <?php post_class('post clearfix'); ?>>
            <h2 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
            <div class="content clearfix">
                <div class="post-info">
                    <p><?php the_time(get_option('date_format')); ?> by <?php the_author_posts_link(); ?></p>
                </div><!--// end .post-info -->
                <div class="entry">
                    <?php the_content( __('Full story…') ); ?>
                </div>
            </div>
        </div><!--// end #post-XX -->

        <?php endwhile; ?>

        <div class="navigation clearfix">
            <div class="alignleft"><?php next_posts_link('« Previous Entries') ?></div>
            <div class="alignright"><?php previous_posts_link('Next Entries »') ?></div>
        </div>

        <?php else: ?>

        <h2 class="post-title">No News in <?php echo apply_filters( 'the_title', $term->name ); ?></h2>
        <div class="content clearfix">
            <div class="entry">
                <p>It seems there isn't anything happening in <strong><?php echo apply_filters( 'the_title', $term->name ); ?></strong> right now. Check back later, something is bound to happen soon.</p>
            </div>
        </div>

        <?php endif; ?>
    </div><!--// end .primary-content -->

    <div class="secondary-content">
        <?php get_sidebar(); ?>
    </div><!--// end .secondary-content -->

<?php get_footer(); ?>
share|improve this question

1 Answer 1

up vote 1 down vote accepted

Looking at your comment, photos and videos aren't custom post types but actually categories. I'm changing my answer to reflect your comment.

You can use pre_get_posts to exclude specific categories in your loop. You can achieve that with $query->set( 'cat=-1,-2,-3' ); This will exclude categories with ID's 1, 2 and 3, and will only show categories with ID's other that 1, 2 and 3.

Just remember, to exclude categories, you have to use the minus(-) sign in front of the category ID.

Here is the complete code that should go into your functions.php to exclude your categories on your taxonomy page

function exclude_category( $query ) {
    if ( !is_admin() && $query->is_tax() && $query->is_main_query() ) {
        $query->set( 'cat', '-1,-2,-3' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.