The MU forums have moved to WordPress.org

List posts by multiple category? (3 posts)

  1. Blandanomics
    Member
    Posted 15 years ago #

    Hi guys, I have been trying to figure out a way to list the last X posts in a given set of categories. So far I have followed the advice on this page to produce this:

    <?php $postslist = get_posts('numberposts=10&category=26,1&orderby=post_date'); foreach ($postslist as $post) : setup_postdata($post); ?> <div>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    </div>
    <?php endforeach; ?>
    </div>

    This outputs the last 10 for cat 26, but not 1. Is there any way to have both?

  2. binh
    Member
    Posted 15 years ago #

    This is kinda interesting. Did you know that category take 1 integer number only?

    How about you try this:

    <?php
    $postslist1 = get_posts('numberposts=10&category=26&orderby=post_date');
    $postslist2 = get_posts('numberposts=10&category=1&orderby=post_date');
    $postslist=array_merge($postslist1, $postslist2);
    foreach ($postslist as $post) : setup_postdata($post); ?> <div>
    <li>
    <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    </div>
    <?php endforeach; ?>
    </div>

    And if you want the code to run quicker, you might want to make 2 separate loop for each list instead of merging them first. That's regarding efficiency :)

  3. Blandanomics
    Member
    Posted 15 years ago #

    This did the trick! Thank you so much!

About this Topic

  • Started 15 years ago by Blandanomics
  • Latest reply from Blandanomics