Title: Getting posts based upon category returning wrong content
Last modified: August 20, 2016

---

# Getting posts based upon category returning wrong content

 *  [dfisch](https://wordpress.org/support/users/dfisch/)
 * (@dfisch)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/getting-posts-based-upon-category-returning-wrong-content/)
 * I’m writing a widget which will get posts based upon the category they are in
   but I am getting results back which belong to different categories.
 * In order to get the categories I’m using
 *     ```
       foreach( $slugs as $slug )
       {
       	if( $slug != '')
       	{
       		$category = get_category_by_slug( $slug );
       		if( $category !== false )
       		{
       			$category_list[] = $category->cat_ID;
       		}
       	}
       }
       ```
   
 * This give me $category_list containing the following data:
    array(2) { [0]=> 
   string(2) “32” [1]=> string(2) “37” }
 * I am then using
 *     ```
       $related_posts = get_posts( array(
       				'numberposts'  => $total_to_return,
       				'category__in' => $category_list
       ));
       ```
   
 * which is returning me $num_to_return posts regardless of which categories they
   belong to.
 * I’ve also attempted to use
 *     ```
       $post_args = array(
       		'posts_per_page' => $total_to_return,
       		'category__in'   => $category_list,
       		'orderby'        => 'post_date',
       		'order'          => 'DESC'
       );
       $get_posts = new WP_Query;
       $related_posts = $get_posts->query( $post_args );
       ```
   
 * which returns the same posts. I’ve poked through wp-includes/query.php and wp-
   includes/post.php to try to figure out what is going on without any luck.

Viewing 1 replies (of 1 total)

 *  Thread Starter [dfisch](https://wordpress.org/support/users/dfisch/)
 * (@dfisch)
 * [14 years, 11 months ago](https://wordpress.org/support/topic/getting-posts-based-upon-category-returning-wrong-content/#post-2144353)
 * I figured out my issue. While I was looping through the $related_posts variable
   I was calling setup_postdata(); when I needed to be calling setup_postdata($post);
 * Everything is working as expected now.

Viewing 1 replies (of 1 total)

The topic ‘Getting posts based upon category returning wrong content’ is closed 
to new replies.

## Tags

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

 * 1 reply
 * 1 participant
 * Last reply from: [dfisch](https://wordpress.org/support/users/dfisch/)
 * Last activity: [14 years, 11 months ago](https://wordpress.org/support/topic/getting-posts-based-upon-category-returning-wrong-content/#post-2144353)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
