WordPress.org

Ready to get started?Download WordPress

Forums

Help with wp_query (2 posts)

  1. Roar
    Member
    Posted 5 years ago #

    Hello,

    I have been reading and researching and sadly, I fear my answer is so simple that people are like "Eh, everyone knows THAT!" and the answer cannot be find by me.

    Here is the code:

    <?php $my_query = new WP_Query('orderby=rand'); ?>
    <?php while ($my_query->have_posts()) : $my_query->the_post();?>
    <li>

    And what I want to query specific categories, the last number of posts in those categories, for example:

    scrolling in order, the last 2 posts from categories 2,4,7,22,100

    Can someone help me know what to write in there?

  2. MichaelH
    Member
    Posted 5 years ago #

    Austin Matzko (filosofo) showed this, I believe on the wp-hackers list:

    $post_ids = array(0);
    foreach( array(2, 4, 7, 22, 100) as $cat_id ) {
            if ( $posts = get_posts(array('cat' => $cat_id, 'showposts' => 2)) ) {
                    $first = array_shift($posts);
                    $post_ids[] = $first->ID;
            }
    }
    query_posts(array('post__in' => $post_ids));
    //your loop here

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags