Title: Using loop functions to call specific data
Last modified: August 19, 2016

---

# Using loop functions to call specific data

 *  Resolved [Nizuya](https://wordpress.org/support/users/nizuya/)
 * (@nizuya)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/)
 * I’m using word press to control the news feed on my website. I have had no problem
   creating my own template, which works great as the news homepage of the website.
 * However, I want to have posts from specific categories show up on the main home
   of my website (outside of the news section). For example I have about 7 categories
   but I want only the latest post from categories 3 and 4 to show up on the homepage
   of my website.
 * I started out writing my own code to query the information from the word press
   database but I would much rather use word press functions.
 * Is there some function I can call that will only get the latest post from a specific
   category?
 * Thanks,
    David

Viewing 4 replies - 1 through 4 (of 4 total)

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/#post-1399169)
 *     ```
       <?php
       //get 2 cats, then display 3 posts in each cat
       $taxonomy = 'category';//  e.g. post_tag, category
       $param_type = 'category__in'; //  e.g. tag__in, category__in
       $term_args=array(
         'orderby' => 'name',
         'order' => 'ASC',
         'include' => '3,4'
       );
       $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' => -1,
             '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 [Nizuya](https://wordpress.org/support/users/nizuya/)
 * (@nizuya)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/#post-1399177)
 * This works great although rather than showing 3 posts from each category it shows
   all of them.
 * Also is there a way to show posts that are in one category but not in another.
 * Example: post 1 belongs to category 3, 4, & 7, and post 2 belongs to category
   3,4. So I only want show posts from category 3 and not 7. So the output would
   only show post 2.
 *  Thread Starter [Nizuya](https://wordpress.org/support/users/nizuya/)
 * (@nizuya)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/#post-1399188)
 * I figured out a way to do it… I’ll post my code in a bit for anyone else who 
   might read.
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/#post-1399190)
 *     ```
       'posts_per_page' => -1,
       ```
   
 * that should be
 *     ```
       'posts_per_page' => 3,
       ```
   

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Using loop functions to call specific data’ is closed to new replies.

## Tags

 * [custom loop](https://wordpress.org/support/topic-tag/custom-loop/)
 * [customization](https://wordpress.org/support/topic-tag/customization/)
 * [template](https://wordpress.org/support/topic-tag/template/)
 * [the-loop](https://wordpress.org/support/topic-tag/the-loop/)

 * 4 replies
 * 2 participants
 * Last reply from: [MichaelH](https://wordpress.org/support/users/michaelh/)
 * Last activity: [16 years, 2 months ago](https://wordpress.org/support/topic/using-loop-functions-to-call-specific-data/#post-1399190)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
