Title: Help needed with query
Last modified: August 21, 2016

---

# Help needed with query

 *  Resolved [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/)
 * Hi guys,
 * I’m working on a specific template for a category page. I have a global function
   defined that retrieves the category id.`$ps_catid = get_query_var(‘cat’);’
 * What i’m trying to do is run a query that shows only posts that are in this current
   category ($ps_catid) but not also in the category of id=46. Some of my posts 
   are in multiple categories you see.
 * Can anyone help produce the code that will show all posts in the current category
   but not ones also in 46?
 * I’ve tried having a go at this on my own but keep getting the dreaded white screen.
   Any help is much appreciated. Thanks

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

 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933894)
 * Hmmm… this is a good one!!
 * Let’s try something like this:
 *     ```
       <?php
       $args=array(
         'category' => $ps_catid,  // Taken from your example above
         'category__not_in' => '46',  // Taken from your example above
         'post_type' => 'post',
         'post_status' => 'publish',
         'posts_per_page' => 10
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
           <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
         endwhile;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
       ```
   
 * No promises as to it working at this point 😉
 * These two lines:
 *     ```
       'category' => $ps_catid,  // Taken from your example above
       'category__not_in' => '46',  // Taken from your example above
       ```
   
 * … should tell it; Get all posts from current category… but exclude the ones that
   are also in category ID 46.
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933912)
 * Thanks Josh,
 * I’ve put your code in and it works!! However, there seems to be a slight problem.
 * I removed the argument line that states ’10 posts per page’ as my wordpress settings
   has it currently showing just 3 which is fine as i’m building the site on my 
   testing server. There are only 2 posts which meet the arguments criteria (ie.
   in the current category and not in 46), and although these are now the only ones
   that are showing, the next_post_link is displaying and linking to a page 2 which
   again shows the same 2 posts!?
 * This is strange as there shouldn’t be a page 2 because there are only 2 posts
   meeting the argument and the settings are set to 3 posts per page. Do you know
   why it would duplicate the same posts and put them on a second page?
 * Thanks. The full code is as follows…
 *     ```
       <?php
       $args=array(
         'category__in' => $ps_catid,  // Taken from your example above
         'category__not_in' => '46'  // Taken from your example above
       );
       $my_query = null;
       $my_query = new WP_Query($args);
       if( $my_query->have_posts() ) {
         while ($my_query->have_posts()) : $my_query->the_post(); ?>
   
                                       <div class="trends_panel">
   
                                                   <div class="trends_header">
   
                                                           <div class="trends_date">
                                                           	<?php the_time('F jS, Y') ?>
                                                           </div>
   
                                                                       <div class="trends_title">
   
                                                                           <h1><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?>
                                                  								</a>
                                                                           </h1>
   
                                                                       </div>
   
                                                                       			<?php $image = get_post_meta($post->ID, 'category image', true);
   
       																		   if($image) : ?>
   
       																		   <a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
                                                                                  	<img src="<?php echo $image; ?>" border="0" class="resize" />
       																		   </a>
   
       																		   <?php endif; ?>
   
                                                                                           <div class="trends_excerpt">
                                                                                               <?php the_excerpt(); ?>
                                                                                           </div>
   
                                                                                                   <div class="trends_more">
                                                                                                   <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">Read More
                                                                                                   </a>
                                                                                                   </div>
                                                   </div>
   
                                       </div>
   
                       																	<?php
         endwhile;
       }
       wp_reset_query();  // Restore global post data stomped by the_post().
       ?>
   
                                                                                        <div class="clear">
                          															     </div>   			
   
                                   <?php previous_posts_link('&laquo; More Recent Posts') ?>
   
                                       <?php next_posts_link('Older Posts &raquo;') ?>
       ```
   
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933944)
 * Just realised that i should have been using the `<?php posts_nav_link(); ?>` 
   rather than the next_post_link given that this is a category page. Unfortunately
   though the problem still persists. It’s still creating a page 2 showing the same
   2 posts.
 *  [Josh](https://wordpress.org/support/users/josh401/)
 * (@josh401)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933945)
 * Sorry, I got sidetracked.
 * What’s the url where you are using this?
 * (If you don’t want to post it here, I’ll provide you a link to my contact form.)
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933960)
 * The site isn’t live yet as i’m currently building it on my testing server using
   mamp.
 * I’ve not come across this problem before and have tried various things but can’t
   seem to solve the problem. Very strange.
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933983)
 * Ok so i have used the following code that i found on the forum from a similar
   post which works. here it is..
 *     ```
       <?php query_posts( array( 'category__not_in' => array(46), 'category_name' => 'nike', 'paged' => $paged, 'orderby' => 'title' ) );?>
   
       <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>
       ```
   
 * The only thing that needs changing is the category name. At the moment i put 
   in a random category to test it, in this case ‘nike’, which works fine, but the
   page will need to show different posts according to what category was clicked
   on.
 * I tried to put in my global parameter of $ps_catid or $ps_catname but it doesn’t
   work. It just seems to show all posts from several categories.
 * Anyone able to help?
 *  Thread Starter [paa1605](https://wordpress.org/support/users/paa1605/)
 * (@paa1605)
 * [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933985)
 * Working!!!!
 * For anyone that might stumble upon this problem in the future, i finally got 
   it to work. Here is the code…
 * `<?php query_posts( array( 'category__not_in' => array(46), 'category__in' =>
   array($ps_catid), 'paged' => $paged ) );?>`

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

The topic ‘Help needed with query’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 7 replies
 * 2 participants
 * Last reply from: [paa1605](https://wordpress.org/support/users/paa1605/)
 * Last activity: [12 years, 11 months ago](https://wordpress.org/support/topic/help-needed-with-query/#post-3933985)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
