Title: custom post_type loop
Last modified: August 20, 2016

---

# custom post_type loop

 *  Resolved [pybiru](https://wordpress.org/support/users/pybiru/)
 * (@pybiru)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/)
 * Hi,
 * I have a custom “post_type” called cities and need to loop through all the cites,
   outputting name, thumbnail and excerpt. I have the following but its not working..
 *     ```
       <?php
   
       	$args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'posts_per_page'  => 20, 'post_status' => 'publish');
       	$loop = new WP_Query($args);
   
       	wp_list_pages($args);
   
       	if($loop->have_posts()){
   
                 while ($loop->have_posts()) : $loop->the_post(); ?>
                   <li>
                     <a href='<?php the_permalink() ?>' rel='bookmark' title='<?php the_title_attribute(); ?>'>
                       <?php the_title(); ?>
                     </a>
                     <?php the_excerpt() ?>
                     <?php echo get_the_post_thumbnail($page->ID, 'thumbnail'); ?>
                   </li>
                 <?php
                 endwhile;
                 } //if ($my_query)
           wp_reset_query();*/
   
       ?>
       ```
   

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

 *  [Peter_L](https://wordpress.org/support/users/peter_l/)
 * (@peter_l)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413804)
 * Start by removing wp_list_pages. Then be explicit in ‘what’s not working’.
 *  Thread Starter [pybiru](https://wordpress.org/support/users/pybiru/)
 * (@pybiru)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413814)
 * Thanks for reply Peter. The “wp_list_pages” should have been commented out, I
   had the titles pulling in/ working using it. But I need thumbnails and some content/
   excerpt too.
 * Whats not working, I need the title, thumbnail and excerpt for each city to be
   output in the loop. At the moment nothing is getting output.
 *  [Peter_L](https://wordpress.org/support/users/peter_l/)
 * (@peter_l)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413833)
 * Try this. Use this for the loop. If it works, add the code to get the thumbnail.
 *     ```
       // The Loop
       if($loop->have_posts()) : while($loop->have_posts()) : $loop->the_post(); ?>
       <li>
           	<a href='<?php the_permalink() ?>' rel='bookmark' title='<?php the_title_attribute(); ?>'>
       	<?php the_excerpt(); ?>
       </li>
       <?php endwhile; else: ?>
       <p>Oops, there are no posts.</p>
       <?php wp_reset_postdata(); // reset the query ?>
       <?php endif; ?>
       ```
   
 *  Thread Starter [pybiru](https://wordpress.org/support/users/pybiru/)
 * (@pybiru)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413845)
 * Thanks for help Peter, I was getting syntax errors from your code(probably sloppy
   integration, not great in PHP).
 * I have the following working, but its outputting all the child pages aswell. 
   I just want all the top level pages. I added “depth” => 1, but doesn’t seem to
   work.
 *     ```
       <?php query_posts(array('post_type'=>'cities', 'depth' => 1)); ?>
   
           <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
   
           <h3>
             <a href="<?php echo the_permalink(); ?>"><?php echo the_title(); ?></a>
             <?php the_content(); ?>
           </h3>
   
           <?php endwhile; ?>
       ```
   
 *  [Peter_L](https://wordpress.org/support/users/peter_l/)
 * (@peter_l)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413851)
 * No, stick with wp_query. Just switch everything you had in your first post with
   this:
 *     ```
       <?php
       	$args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'posts_per_page'  => 20, 'post_status' => 'publish');
       	$wp_query = new WP_Query($args);
       ?>
       <?php
       	// The Loop
       	if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
       	<li>
       		<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
       		<?php the_excerpt(); ?>
       	</li>
       	<?php endwhile; else: ?>
       	<p>Oops, there are no posts.</p>
       	<?php wp_reset_postdata(); // reset the query ?>
       	<?php endif; ?>
       ```
   
 *  Thread Starter [pybiru](https://wordpress.org/support/users/pybiru/)
 * (@pybiru)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413853)
 * Ah, great. That’s working beautifully now. One more thing, I have (‘depth’ =>
   1), in the $args array yet it is outputting the child pages too. Is there a differen’t
   way to only get the parent pages?
 * Thanks a million for the help/advice.
 *  [Peter_L](https://wordpress.org/support/users/peter_l/)
 * (@peter_l)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413855)
 * remove depth, use ‘post_parent’ => 0
 *  Thread Starter [pybiru](https://wordpress.org/support/users/pybiru/)
 * (@pybiru)
 * [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413863)
 * working with just parents pages now, the final code…
 *     ```
       <?php
             $args = array('post_type' => 'cities', 'depth' => 1, 'orderby' => 'name', 'post_status' => 'publish', 'post_parent' => 0, 'posts_per_page'=>'-1');
             $wp_query = new WP_Query($args);
           ?>
           <?php if($wp_query->have_posts()) : while($wp_query->have_posts()) : $wp_query->the_post(); ?>
   
               <li>
                 <div class="city">
   
                     <?php the_post_thumbnail(); ?>
   
                     <a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a>
                     <?php the_excerpt(); ?>
   
                 </div>
               </li>
   
             <?php endwhile; else: ?>
               <p>Oops, something has gone wrong. No cities currently available.</p>
               <?php wp_reset_postdata(); // reset the query ?>
             <?php endif; ?>
       ```
   

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

The topic ‘custom post_type loop’ is closed to new replies.

## Tags

 * [custom post type](https://wordpress.org/support/topic-tag/custom-post-type/)
 * [loop](https://wordpress.org/support/topic-tag/loop/)
 * [post_type](https://wordpress.org/support/topic-tag/post_type/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 8 replies
 * 2 participants
 * Last reply from: [pybiru](https://wordpress.org/support/users/pybiru/)
 * Last activity: [13 years, 2 months ago](https://wordpress.org/support/topic/custom-post_type-loop/#post-3413863)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
