Check category in wp loop

I have a wp query loop. I want to check if the post belongs to some categories. I can get the category using the_category(). I have tried

if(the_category()==`car`){do somthing}

and how to push all the remaining posts except the car category after to all the ‘car’ category.

Related posts

Leave a Reply

2 comments

  1. You can run two queries here. The first query gets all posts from the car caregory. The second query gets all other posts except posts from the car category. Just remember to change CATID FOR CAR with the id of the car category, and don’t forget the minus sign before the ID in the second query. The minus sign means exclude.

    You can read more on this in the codex: WP_Query

    $do_not_duplicate = array();
    
    $args = array(
       'cat' => CATID FOR CAR
    );
    
    $carargs = new WP_Query( $args );
    
    if( $carargs->have_posts()):
    
      while ($carargs->have_posts()) : $carargs- >the_post(); 
      $do_not_duplicate[] = $post->ID; 
    
    <----your loop---->
    
      endwhile; 
    
    endif;
    
    wp_reset_postdata(); 
    
    
    $args2 = array(
       'cat' => -CATID FOR CAR,
       'post__not_in' => $do_not_duplicate
    );
    
    $restargs = new WP_Query( $args2 );
    
    if( $restargs->have_posts()):
    
      while ($restargs->have_posts()) : $restargs- >the_post(); 
       $do_not_duplicate[] = $post->ID; 
    
    <----your loop---->
    
      endwhile; 
    
     endif;
    
    wp_reset_postdata();