Getting posts from categories array

I have some certain categories’ ids. I want to loop this categories and last 3 posts in one time. I try this but only come one category from array.

<?php
    $args = array(
    'cat'      => 48,43,49,46,47,44,51,50,42,
    'order'    => 'ASC',
    'showposts' => 3
        );
query_posts($args);
?>
<?php while (have_posts()) : the_post(); ?>
<?php the_title(); ?>
<?php endwhile; ?>

Related posts

Leave a Reply

5 comments

  1. For some reason ‘cat’ did not work. We used

    'category__in' => array( 2, 6 ),
    

    and it workined fine.

    The completed working code:

    <?php
    // -----------------------------
    $args = array(
        'post_type' => 'post',
        'order' => 'ASC',
        'category__in' => array(2,6)
        );
    $query = new WP_Query( $args );
    ?>
    
  2. You can get All Posts in a Category which one you want publish.

    query_posts( array ( 'category_name' => 'my-category-slug', 'posts_per_page' => -1 ) );
    

    You can find post as per your expectation by.

    query_posts( array ( 'category_name' => 'carousel', 'posts_per_page' => 10, 'orderby' => 'date', 'order' => 'DESC' ) );
    
  3. According to your code. Update —

    <?php
        $args = array(
        'cat'      => [48,43,49,46,47,44,51,50,42], //change here array
        'order'    => 'ASC',
        'posts_per_page' => 3 //showposts deprecated now
            );
    query_posts($args);
    ?>
    <?php while (have_posts()) : the_post(); ?>
    <?php the_title(); ?>
    <?php endwhile; ?>
    <?php wp_reset_query(); ?> // you should reset your query