WordPress.org

Ready to get started?Download WordPress

Forums

Category__and with array in single page (5 posts)

  1. Zakir Sajib
    Member
    Posted 2 years ago #

    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.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    Does $cur_cat_ hold all the ID's of categories.?

    test this by adding a line after or instead of
    echo $cur_cat_; // this displays Array

    print_r( $cur_cat_ );

    this should work:

    global $post;
    $cat_array = get_the_category($post->ID);
    $cur_cat_ = array();
    	foreach($cat_array as $cat):
    		$cur_cat_[] = $cat->term_id;
    	endforeach;
    	print_r( $cur_cat_ ); //test the array
  3. Zakir Sajib
    Member
    Posted 2 years ago #

    Thanks alchymyth
    it displays:

    Array ( [0] => 3 [1] => 30 [2] => 24 )

    Which is basically the category ID's.

    Now the syntax for category__and is

    'category__and' => array(1, 2, 3)

    So if put my array which is $cur_cat_

    'category__and' => $cur_cat_

    which means: 'category__and' => Array ( [0] => 3 [1] => 30 [2] => 24 )

    What I need now is this format:
    'category__and' => array(3, 30, 24)

    Any clue?

  4. Zakir Sajib
    Member
    Posted 2 years ago #

    It worked.

    I now used category__in rather than category__and

    Thanks once again to Alchymyth.

  5. webmasterobservatoriobioetica
    Member
    Posted 1 year ago #

    Hi, I have a similar problem.

    I'm making a page, and I need to order some posts belonging to two specific categories, for this, I looked around, and I have to use the category__and. So much perfect.

    The problem is that if I use the post category__and not sorted from newest to oldest, it is sorted by date of creation, the first post created the first and last end.

    code used:

    CATEGORY__IN = WORK:

    if( trim( $pagination ) == 'true' ) {
    	$paged = mysite_get_page_query();
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__in' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'posts_per_page' => $showposts,
    		'paged' => $paged,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    
    } else {
    
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__in' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'showposts' => $showposts,
    		'nopaging' => 0,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    }

    WITH CATEGORY__AND = NOT WORK

    if( trim( $pagination ) == 'true' ) {
    	$paged = mysite_get_page_query();
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__and' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'posts_per_page' => $showposts,
    		'paged' => $paged,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    
    } else {
    
    	$blog_query->query(array(
    		'post__in' => $post_in,
    		'category__and' => $category_in,
    		'tag__in' => $tag_in,
    		'post_type' => 'post',
    		'showposts' => $showposts,
    		'nopaging' => 0,
    		'offset' => $offset,
    		'ignore_sticky_posts' => 1,
    'orderby' => 'date',
    'order' => 'DESC',
    	));
    }

    If I use category__in, all right, if I use category__and, not working :(

    Can you help me?? Thanks

Topic Closed

This topic has been closed to new replies.

About this Topic