0
<?php
 $cat_name=array('Breaking News Stories', 'Call-out', 'Featured Story', 'Standard Stories');
 $count=count($cat_name);
 /*Get category_id by its name*/

 for($i=0; $i < $count; $i++)
  {
  $cat_id5.="'".get_cat_ID($cat_name[$i])."',";
  //$cat_id4.="'".$cat_id5;
  }
 echo  $id1=rtrim($cat_id5,',');
 //query_posts("cat=$catIDs&showposts=4");
 $args=query_posts(array(
                  'category__in' => array($id1),
                  'posts_per_page' => -1,
                // 'category_name' => array('breaking-news-stories',);
                 //'category_name' =>  array('category_name' ,'breaking-news-stories')

         ));
  $posts_array = get_posts($args);
  print_r($posts_array);
 ?>

When I'm using the above code, I'm getting the first 5 posts. I want to get all the post by the above mentioned ID. Guide me how to get that! I'm new to wordpress.

  • Are you trying to get a list of posts under each of the categories. Just to note, in my previous answer I've adviced not to use query_posts and also category_name takes a string, not array. – Pieter Goosen Sep 10 '14 at 8:07
  • if i use WP_Query(),it shows Fatal error</b>: Call to undefined function WP_Query() in – PHP Learner Sep 10 '14 at 8:09
  • Yes I'm trying to get all post under each category – PHP Learner Sep 10 '14 at 8:11
  • Even I'm getting the same result while using the WP_Query() – PHP Learner Sep 10 '14 at 8:13
  • Ok, then you are using it wrongly, :-). I will post a proper solution, just let me know first, do you need to make a list of these categories with all the posts under it – Pieter Goosen Sep 10 '14 at 8:13
1

Your use of queries is completely wrong, and as stated before, query_posts should never be used. The method you are using to retrieve you category ID's are also a waste of resources. It would save much more if you knew the ID's.

I would also look at get_categories to retrieve my category details and passing that to my custom query.

METHOD 1

If these categories are all the categories in your blog, you don't need to know the category name or ID of the specific category to continue. You can retrieve that all with get_categories (check out all the available arguments and how they are used). In your foreach loop, you can then just pass the cat_ID to the cat parameter in your WP_Query

$categories = get_categories();
foreach ($categories as $category) {
    $query = new WP_Query( 'posts_per_page=1&cat=' . $category->cat_ID );

    if($query->have_posts()){
        while($query->have_posts()) {
        $query->the_post();
           //WHATEVER YOU NEED TO DISPLAY
        }
    }
    wp_reset_postdata();
}

METHOD 2

If you just need to get these few categories, and don't have the ID's, you can add the names in an array and use get_cat_ID to get the ID's and then pass it in the same way as METHOD 1

$cat_names=array('Breaking News Stories', 'Call-out', 'Featured Story', 'Standard Stories');

foreach ( $cat_names as $cat_name ) {
    $cat_id = get_cat_ID( $cat_name );

    $query = new WP_Query( 'posts_per_page=-1&cat=' . $cat_id );

     // REST IS SAME AS IN METHOD 1

} // end of foreach loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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