Title: get_posts returns all posts
Last modified: August 20, 2016

---

# get_posts returns all posts

 *  [candybrie](https://wordpress.org/support/users/candybrie/)
 * (@candybrie)
 * [13 years, 6 months ago](https://wordpress.org/support/topic/get_posts-returns-all-posts/)
 * I’m trying to display an image as a link for a category, and I’d prefer that 
   image to be one that is attached to a post in that category. Currently it just
   displays the same image for all of them.
 * This is what I have right now
 *     ```
       <?php $args = array( 'post_type' => 'post' );
       $categories=get_categories($args);
       $thumbURL;
       $posts;
       $id;?>
   
       <?php foreach($categories as $category) : ?>
   
       	<?php
       	$args = array(
       		'suppress_filters' => false,
       		'post_type' => 'attachment',
       		'numberposts' => -1,
       		'category' =>$category->term_ID,
       		'order'    => 'ASC',
       		);
       	$posts=get_posts( $args );
   
       	if ($posts){
       		foreach($posts as $post){
       			if ( wp_attachment_is_image() ){
       				if( $thumbURL=wp_get_attachment_thumb_url() ){
       					break;
       				}
       			}
       		}
       	}
       	?>
       		<a class="categoryNav" href="<?php echo get_category_link($category->term_id); ?>" title="<?php echo $category->name; ?>">
       		<img class="categoryNav" src="<?php echo $thumbURL; ?>" alt="<?php echo $category->name; ?> : <?php echo $category->description; ?>" />
       	</a>
   
       <?php endforeach ?>
       ```
   
 * I’m pretty new to PHP and wordpress, so any feedback would probably be helpful
   to me.

The topic ‘get_posts returns all posts’ is closed to new replies.

## Tags

 * [categories](https://wordpress.org/support/topic-tag/categories/)
 * [get_posts](https://wordpress.org/support/topic-tag/get_posts/)

 * 0 replies
 * 1 participant
 * Last reply from: [candybrie](https://wordpress.org/support/users/candybrie/)
 * Last activity: [13 years, 6 months ago](https://wordpress.org/support/topic/get_posts-returns-all-posts/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
