Title: WP_Query not working
Last modified: August 22, 2016

---

# WP_Query not working

 *  Resolved [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/)
 * Could anyone explain why this query isn’t working?
    It still shows the post with
   category name ‘homepage’…
 *     ```
       <?php
       	$query = new WP_Query( 'category_name=-homepage');
       ?>
       <?php if ( $query->have_posts() ) : ?>
       	<?php while ( have_posts() ) : the_post(); ?>
       		<?php
       			get_template_part( 'content', 'news' );
       		?>
       	<?php endwhile; ?>
       	<?php the_posts_navigation(); ?>
       	<?php else : ?>
       		<?php get_template_part( 'content', 'none' ); ?>
       	<?php endif; ?>
       ```
   

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

 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901153)
 * Hello,
 * I don’t understand what you want, but i Think is homepage and not -homepage
 * No ?
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901157)
 * I want to exclude the posts tagged with _homepage_.
    I also tried `$query = new
   WP_Query( 'cat=-11' );` but it isn’t working either.
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901159)
 * Are you try category__not_in in your query ?
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901163)
 * Also not working:
    `$query = new WP_Query( 'category__not_in => 11' );`
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901164)
 * Ok
 * Ihave found this
 *     ```
       <?php
       $temp_query = $wp_query;
       $exclude = get_cat_ID('showreel');
       query_posts("cat=-$exclude&post_type=projects&showposts=4&order=ASC");?>
       ```
   
 * Try width this exemple. Do an variable $exclude and add -$exclude to your query…
 * I hope it will work !
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901167)
 *     ```
       <?php
       	$args = array(
       	'category_name' => 'homepage',
       	);
       	$query = new WP_Query( 'cat=-$args' );
       ?>
       ```
   
 * isn’t working either 🙁
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901168)
 * I don’t know if is a googd solution but you can do a pre get post if you dont
   want to show posts by the category…
 * In your functions.php
 *     ```
       function demo_exclude_category( $wp_query ) {
   
           // Add the category to an array of excluded categories. In this case, though,
           // it's really just one.
           $excluded = array( '-1' );
   
           // Note that this is a cleaner way to write: $wp_query->set('category__not_in', $excluded);
           set_query_var( 'category__not_in', $excluded );
   
       }
       add_action( 'pre_get_posts', 'demo_exclude_category' );
       ```
   
 * Change the good ID of your category…
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901170)
 * Maybe, but I only want it on one page… 🙂
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901172)
 * Hello
 * I have try it and it’s work !
 *     ```
       <?php
           query_posts(array(
               'post_type' => 'post',
               'showposts' => 5,
               'cat' => '-1'
           ) );
       ?>
       <?php if ( $query->have_posts() ) : ?>
       	<?php while ( have_posts() ) : the_post(); ?>
       		<?php
       			get_template_part( 'content', 'news' );
       		?>
       	<?php endwhile; ?>
       	<?php the_posts_navigation(); ?>
       	<?php else : ?>
       		<?php get_template_part( 'content', 'none' ); ?>
       	<?php endif; ?>
       ```
   
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901177)
 * Still not working: suddenly I get pages in the newsfeed :p
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901196)
 * `You also need to change while ( have_posts() ) : the_post(); to while ( $query-
   >have_posts() ) : $query->the_post();`
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901207)
 * it’s work now ?
 *  Thread Starter [MathCiet](https://wordpress.org/support/users/mathciet/)
 * (@mathciet)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901210)
 * Yes! 🙂
    Thank you for your help!
 *  [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * (@viamultimedia)
 * [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901213)
 * You’re welcome ! I haven’t seen the $query->have_posts(). I’m tired !
    Happy 
   it’s work have a good day !

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

The topic ‘WP_Query not working’ is closed to new replies.

## Tags

 * [query](https://wordpress.org/support/topic-tag/query/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 14 replies
 * 2 participants
 * Last reply from: [Viamultimedia](https://wordpress.org/support/users/viamultimedia/)
 * Last activity: [11 years, 3 months ago](https://wordpress.org/support/topic/wp_query-not-working/#post-5901213)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
