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'd like to list my 10 most recent posts but from certain categories only - anyone can tell me how to do this?

thanks

share|improve this question
    
You should accept some of your answers - your accept rate is very low –  anu Mar 8 '11 at 12:05
add comment

3 Answers

up vote 6 down vote accepted

simply add this above your loop

$args = array(
    'posts_per_page' => 10,
    'category__in' => array( 2, 6 ), //change and add the category ids here
    'orderby' => 'date',
    'order' => 'ASC')
query_posts($args);

and you can read more about query_posts Parameters http://codex.wordpress.org/Function_Reference/WP_Query#Parameters

Update

By popular demand :) here is another example to doing the same but using tax_query

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'category',
            'field' => 'slug',
            'terms' => array('category1','category2') ////change and add the category slugs here
        )
    )
    'posts_per_page' => 10,
    'orderby' => 'date',
    'order' => 'ASC')
query_posts($args);
share|improve this answer
    
I'll give you a +1 if you update your answer to include an additional example of doing the same with tax_query using slugs .. ;) –  t31os Mar 8 '11 at 12:31
    
@t31os - Only because you asked so nicely :) –  Bainternet Mar 8 '11 at 13:21
    
Nice one, +1'ed!.. :) –  t31os Mar 8 '11 at 13:34
    
10x for the "Popular demand" +1'ed too ;) –  Sagive SEO Apr 2 '12 at 15:29
add comment

Have a look at the category query parameters here:

http://codex.wordpress.org/Function_Reference/WP_Query#Category_Parameters

share|improve this answer
add comment

The easiest way to do this is to have use the category name in the query before the loop starts.

<?php query_posts($query_string . '&category_name='Your Category, Another Category'); ?>

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

I haven't tested it but should work.

Also check out http://codex.wordpress.org/Function_Reference/query_posts

share|improve this answer
add comment

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.