Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm trying to only display posts from a certain category in a widget. I've tried it in a few different ways but it is still showing posts from all categories. Here's my code at the moment:

$posts = get_posts(array ('category'=>6, 'numberposts'=>$number, 'order'=>'DESC', 'orderby'=>'date'));
        foreach ($posts as $post){
            setup_postdata($post);....

Thanks.

share|improve this question

2 Answers 2

Did you try the example given in the codex?

<?php
global $post;
$args = array( 'numberposts' => $number, 'category' => 6, 'order' => 'ASC', 'orderby' => 'post_date'  );
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php endforeach; ?>

This a modified version that should suit your needs. Are you also certain that this category exists?

Edit:

<?php $temp = $wp_query; $wp_query= null; $wp_query = new WP_Query('showposts=' . $numberposts . '&cat=6&orderby=date&order=ASC'); ?>
<?php while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
        <!-- your content -->
<?php endwhile; ?>
<?php $wp_query = null; $wp_query = $temp; ?>
share|improve this answer
    
The category definitely exists. I did try this code before and I double checked then with the code you provided but it doesn't work. Interestingly, it also didn't change the order despite it being changed from DESC to ASC. –  Will Jun 15 '11 at 16:15
    
Try the code in my edit above, to see if it helps. –  kevin Jun 15 '11 at 17:03

I guess you got 2 loops and maybe one before. Try to use wp_reset_query() before or rewind_posts().

share|improve this answer
    
No luck. Thanks though. –  Will Jun 15 '11 at 16:38
    
Try to use 'category__in' as parameter. But dunno if all query_posts()/WP_Query arguments work there. –  kaiser Jun 15 '11 at 16:39

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.