Category__and with array in single page
-
Hi i have a problem with category__and in single page.
I want to find out all the posts which belong to current categories of current post. So if my current post has 3 categories i want to use those cateogires id to display all other posts which belong to those 3 categories.
So what I have done so far:
I used get_the_category function to get the current categories and then I used following method to get the category ID.
In single.php:
global $post; $cur_post_id = $post->ID; $cat_array = get_the_category($cur_post_id); //$cur_cat_id = $cat_array[0]->cat_ID;
Also I modified above code doing following:
$x=0; $i=1; $cur_cat_[0]='1'; foreach($cat_array as $cate): $cur_cat_[$x] = $cate->cat_ID; $x++; $i++; endforeach; $cur_cat_ = array_filter($cur_cat_); $cur_cat_ = array_values($cur_cat_); echo $cur_cat_; // this displays Array
And later I used query to display related 5 products.
$args1= array( 'posts_per_page'=>5, 'post-type' => 'post', 'catetgory__and' => $cur_cat_, 'post__not_in' => array($cur_post_id), 'ignore_sticky_posts' => 1 ); if (have_posts()) : ?> /// <?php while (have_posts()) : the_post(); ?> // <?php endwhile; wp_reset_query();?>
My problem is ‘catetgory__and’ => $cur_cat_,
Does $cur_cat_ hold all the ID’s of categories.? I think it should.
But some reason its not displaying required posts as it should.Any clue??
Thanks in advance.
- The topic ‘Category__and with array in single page’ is closed to new replies.