• Resolved Nith

    (@nith)


    Hello all,

    Just wondering, how do I create a query that will:

    Pull the 2 most recent posts from each of the category id’s: 2,3 & 4.

    Ty.

Viewing 3 replies - 1 through 3 (of 3 total)
  • WordPress Recent Posts from specific category

    Just change the category number and number of posts.

    <?php
    //get selected categories then display 2 posts in each term
    $taxonomy = 'category';
    $param_type = 'category__in';
    $term_args=array(
      'include'=> '2,3,4',
      'orderby' => 'name',
      'order' => 'ASC'
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'post',
          'post_status' => 'publish',
          'posts_per_page' => 2,
          'caller_get_posts'=> 1
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'List of Posts in '.$taxonomy .' '.$term->name;
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
           <?php
          endwhile;
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>
    Thread Starter Nith

    (@nith)

    Thanks Michael,

    Works perfectly.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Question about a query’ is closed to new replies.