I trying to get all the latest post from every category, but I discovered I must ignore one of them. I don't want to show it at all. My problem is I don't know what syntax to write ifelse in this code.

Here is my code :


  $latest_cat_post_6 = 
   new WP_Query( array('posts_per_page' => 1,'category__in'=>$output_categories[$i]));

   if($latest_cat_post_6 == 48){
             //do nothung 
             } else {
  if( $latest_cat_post_6->have_posts() ) :
  while( $latest_cat_post_6->have_posts() ) : $latest_cat_post_6->the_post();
  • 1
    oky ..... thanx @Pieter Goosen
    – Hadi Omary
    Aug 6, 2014 at 8:13

1 Answer 1


Your code doesn't make sense to me. The easiest way to do this is to use get_categories() to get all the categories, and use the exclude parameter to exclude the category that you don't need. You can then feed that back into a new WP_QUERY


$categories = get_categories( 'exclude=ID_OF_CATEGORY')

foreach ($categories as $category) {

  $new_query = new WP_Query( 'posts_per_page=1&cat=' . $category->cat_ID );

    if($new_query->have_posts()) : 
      while($new_query->have_posts()) : $new_query->the_post();




  • it is work with me ..... yes your one easier but you don't answer how to ignore or skip one of category in your cod
    – Hadi Omary
    Aug 6, 2014 at 9:24
  • I did explain it, please reread my answer carefully. $categories = get_categories( 'exclude=ID_OF_CATEGORY'). It is here that you must swop ID_OF_CATEGORY with the ID of the category that you want to exclude Aug 6, 2014 at 9:33
  • it is work >>>>>> thanx
    – Hadi Omary
    Aug 6, 2014 at 12:54
  • My pleasure. Enjoy :-) Aug 6, 2014 at 12:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.