WordPress.org

Ready to get started?Download WordPress

Forums

Excluding Post Categories from a page (4 posts)

  1. cre8ve1
    Member
    Posted 1 year ago #

    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

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Try using the 'category__not_in' argument:

    $blog_query = new WP_Query( array( 'category__not_in' => array(12, 25), 'post_type' => 'post', 'paged' => $paged ) );
  3. cre8ve1
    Member
    Posted 1 year ago #

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

  4. ubaidullahbutt
    Member
    Posted 1 year ago #

    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

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.