Support » Plugins » 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)
  • 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.