Support » Fixing WordPress » Excluding Post Categories from a page

  • Hi,

    I’m a WP newbie and I’ve been searching wordpress.org for a solution and have yet to come across one that actually works for my child theme.

    I have a blog page and I want to exclude two categories from showing: they have IDs of 12 and 25. The following is my code and I think I’m either coding it wrong (as I’m new to php) OR I have it in the wrong place.

    <?php
    	global $wp_query, $paged;
    
    	if ( get_query_var( 'paged' ) ) {
    		$paged = get_query_var( 'paged' );
    	}
    
    	elseif ( get_query_var( 'page' ) ) {
    		$paged = get_query_var( 'page' );
    	}
    
    	else {
    	        $paged = 1;
    	}
    
    	$blog_query = new WP_Query( array( 'cat' => -12,-25, 'post_type' => 'post', 'paged' => $paged ) );
    	$temp_query = $wp_query;
    	$wp_query = null;
    	$wp_query = $blog_query;
    
    	if ( $blog_query->have_posts() ) : ?>
    
                    <header class="page-header">
    			<h1 class="page-title"><?php the_title(); ?></h1>
                    </header><!-- .page-header -->
    
                    <?php catcheverest_content_nav( 'nav-above' ); ?>
    
    		<?php /* Start the Loop */ ?>
    		<?php while ( $blog_query->have_posts() ) : $blog_query->the_post(); ?>
    
    		<?php
                       get_template_part( 'content', get_post_format() );
                    ?>
    
                    <?php endwhile; ?>
                    <?php catcheverest_content_nav( 'nav-below' ); ?>	
    
    	<?php else : ?>   
    
    		<?php get_template_part( 'no-results', 'archive' ); ?>
    
    	<?php endif;
    	$wp_query = $temp_query;
    	wp_reset_postdata();
    	?>

    Any help would be appreciated!!

    Thanks in advance,
    Kathryn

Viewing 3 replies - 1 through 3 (of 3 total)
  • vtxyzzy

    (@vtxyzzy)

    Try using the ‘category__not_in’ argument:

    $blog_query = new WP_Query( array( 'category__not_in' => array(12, 25), 'post_type' => 'post', 'paged' => $paged ) );
    cre8ve1

    (@cre8ve1)

    Thank you so much vtxyzzy! That worked. I appreciate you taking the time to help me out.

    ubaidullahbutt

    (@ubaidullahbutt)

    You can also use something like that

    $query = new WP_Query( 'author=-12' );

    It will display all posts except those from an author(singular) by prefixing its id with a ‘-‘ (minus) sign

    For more information, look at http://codex.wordpress.org/Class_Reference/WP_Query

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Excluding Post Categories from a page’ is closed to new replies.