Title: query_posts in sidebar
Last modified: August 18, 2016

---

# query_posts in sidebar

 *  [thebindlestick](https://wordpress.org/support/users/thebindlestick/)
 * (@thebindlestick)
 * [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/)
 * I have one specific category (let’s call it photography) that I do not want to
   display in my sidebar with the 10 most recent posts, but I do want to display
   a list of recent posts in the photography category below the main list… exactly
   how do I use query_posts to select only posts from the photography catagory, 
   and how do I eliminate that catagory from my main list? I used the code below
   but it does not work. I am using WP 2.1… trying to avoid another plugin if possible,
   is it possible to just code this setup?
 *     ```
       <?php query_posts("cat=5"); ?>
       <?php if (have_posts()) : ?>
   
       <?php while (have_posts()) : the_post(); ?>
   
       <div class="post" id="post-<?php the_ID(); ?>">
   
       <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>"><?php the_title(); ?></a>
   
       <?php endwhile; ?>
       <?php endif; ?>
       </div>
       ```
   

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

 *  Thread Starter [thebindlestick](https://wordpress.org/support/users/thebindlestick/)
 * (@thebindlestick)
 * [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/#post-576734)
 * anyone?
 *  [JaneLitte](https://wordpress.org/support/users/janelitte/)
 * (@janelitte)
 * [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/#post-576803)
 * This is the code that I used. I stole it from [Rhymed Code](http://rhymedcode.net/)
 * `<?php query_posts( 'category_name=asides&showposts=4' ); ?>`
    `<?php if( have_posts()):?
   >`
 * `<ul class="asides">`
    `<?php while( have_posts() ) : the_post(); ?>` `<li id
   ="post-<?php the_ID(); ?>">` `<span class="aside">` `<a href="<?php the_permalink()?
   >" rel="bookmark" title="Permalink: <?php the_title(); ?>">` <?php the_title();?
   >` `</a>` `</span>` `<br/>` `<?php echo wptexturize(the_excerpt_reloaded(15,'','',
   false)); ?>` <span class=”aside”>(<?php comments_popup_link (‘0′,’1′,’%’,’C’)?
   >)` `</span>` `</li>` `<?php endwhile; ?>` `</ul>` `<?php endif; ?>`
 *  [JaneLitte](https://wordpress.org/support/users/janelitte/)
 * (@janelitte)
 * [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/#post-576804)
 * Oh, and in the index.php file, I have
 * `<?php`
    `if (is_home()) {` `$paged = (get_query_var('paged')) ? get_query_var('
   paged') : 1;` `query_posts("cat=-26&paged=$paged");` `}` `?>`
 * (before the If (have_posts() )
 *  Thread Starter [thebindlestick](https://wordpress.org/support/users/thebindlestick/)
 * (@thebindlestick)
 * [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/#post-576805)
 * The solution I ended up with after two days of banging my head into my monitor
   is that The Lop in my index and The Loop in my sidebar were interfering with 
   eachother. I found this solution and it is the only thing I can get to work; 
   see first and last lines of code specifically:
 * <?php $temp_query = $wp_query; ?>
    <?php query_posts(‘cat=-5’); ?>
 * <?php if (have_posts()) : ?>
 * <?php while (have_posts()) : the_post(); ?>
 * — do stuff —
 * <?php endwhile; ?>
 * <?php endif; ?>
    <?php $wp_query = $temp_query; ?>
 * Can anyone see why this is a bad idea in WP 2.1?

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

The topic ‘query_posts in sidebar’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [thebindlestick](https://wordpress.org/support/users/thebindlestick/)
 * Last activity: [19 years ago](https://wordpress.org/support/topic/query_posts-in-sidebar/#post-576805)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
