• I have a query to loop thorough all the posts in a particular category. Each post is also assigned to another category. I’m able to output each post with just its alternate category, but need posts grouped by their category (not listed each time), and also I need to order the categories.

    Here’s the current code (volume-4-number-1 is the slug for category 21):

    <?php
    
    query_posts(array('category_name' => 'volume-4-number-1', 'posts_per_page' => -1 )); 
    
    while ( have_posts() ) : the_post();
            echo '<li>';
    
    	foreach((get_the_category()) as $cat) {
        	if ($cat->cat_ID !='21')
           	echo '<a href="'.get_category_link($cat->cat_ID).'">' . $cat->cat_name . '</a>';
    }
    	echo '<br />';
    	the_title();
    	echo '</li>';
    endwhile;
    // Reset Query
    wp_reset_query();
    
    ?>

    SO, I need to have the category name, then below show each post in the category.

    I also need to order the category names. SO, out put should be:

    Category 1
    post
    post
    post
    Category x
    post post

    Right now it’s
    Category 1
    post
    Category 1
    post
    Category 1
    post
    Category x
    post
    Category x
    post

    Can someone please help?

    Thanks!

  • The topic ‘order and looping through query’ is closed to new replies.