• I am trying to implement the exclude by category name into loop (underscore _s theme)
    and I have this so far , where and how to I make it work?

    <?php wp_reset_postdata(); // Resetting previous WP query ?>
    
              <?php
    		$args = array( 'post-type' => 'post' );
    
                    $exclude = get_cat_ID('featured');
    	        $exc = 'cat=-'.$exclude;
    
                    $the_query = new WP_Query ( $args, $exc );
    		?>
    
    		<?php if ( have_posts() ) : ?>
    		    <?php while ( $the_query -> have_posts() ) : $the_query -> the_post(); ?>
    
    		        <?php get_template_part( 'content' , 'landing' ); ?>
    
    			<?php endwhile; ?>
    		<?php endif; ?>

    What am I doing wrong? Query still shows posts with featured category.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    You could try:

    // get the id number
    $featured = get_cat_ID( 'featured' );
    $nonfeatured = new WP_Query ( 'cat=-' . $featured );

    That would be one way I can think of but that is currently untested. Have you read through how the WP_Query works?

    Thread Starter Anneka

    (@anneka)

    Thanks for your reply, I had similar code except I have one more connecting variable in between. It didn’t work the first time, but I tried same code second time but renamed the second variable and it worked. I don’t know but possible that php was getting confused by $exclude and $exc ?

    working code is like this:

    $args = array( 'post-type' => 'post' );
    
      $exclude = get_cat_ID('featured');
      $nonfeat = 'cat=-'.$exclude;
      $non_featured = new WP_Query ( $nonfeat, $args);

    Here is tutorial how to exclude category 🙂

    http://vpstutor.net/how-to-exclude-category-posts-in-wordpress/

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

The topic ‘Exclude Category by name, underscore _s’ is closed to new replies.