2

i'd like to list my 10 most recent posts but from certain categories only - anyone can tell me how to do this?

thanks

6

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);
  • 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
0

Have a look at the category query parameters here:

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

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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