0

This question already has an answer here:

I'm trying to display a list of categories sorted by name and all posts of each category inside it. Like this:

  • Category A
    Post B
    Post F
    Post Z
  • Category B
    Post A
    Post C
    Post G
  • Category C
    Post D
    Post E
    Post H


I managed to display categories list, but it is only sorted by post names, like this:

  • Category B
    Post A
    Post C
    Post G
  • Category A
    Post B
    Post F
    Post Z
  • Category C
    Post D
    Post E
    Post H

    This is my code:

    $args = array('post_type' => 'projetos');
    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()) : $my_query->the_post(); ?>
        <?php foreach((get_the_category()) as $category) {echo $category->cat_name . ' ';} ?><br>
        <a href="<?php the_permalink() ?>"><?php the_title(); ?></a><br>
        <?php
        endwhile;
    }
    
    wp_reset_query();
    
    ?>  
    

How can i sort it properly?

marked as duplicate by s_ha_dum Apr 16 '15 at 22:06

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

EDIT:

Well, it seems the following approach is a bit DB consuming. Pieter Goosen posted a very interesting alternative. Since this approach has already been accepted as a correct answer I don't know, it it is possible to mark this question still as a duplicate. I flagged my answer so a moderator might have a look. Thanks.


I first collect all data in an multidimensional array. The subarrays are the categories.

So, I first load all categories with get_categories(). They are ordered by title. I load all of them into $post_categories while the array key is the ID of the category. The category information like the name will be stored in $post_categories[ category_id ]['cat'].

After this I run the query, but I make sure, it is ordered by title. Now, I get all the categories the post is attached to and I save the post in $post_categories[ category_id ]['posts'].

Now I have an multidimensional Array, which I just output with two foreach() loops.

$args = array( 
        'type' => 'projetos' 
    );
    $all_categories = get_categories( $args );
    $post_categories = array();
    foreach( $all_categories as $category )
        $post_categories[ $category->term_id ]['cat'] = $category;
    $args = array(
        'post_type' => 'projetos',
        'orderby' => 'title',
        'order' => 'asc' 
    );
    $my_query = new WP_Query( $args );
    if( $my_query->have_posts() ) {
        while ($my_query->have_posts()){ 
            $my_query->the_post();
            $p = array();
            $p['permalink'] = get_the_permalink();
            $p['title'] = get_the_title();
            foreach( get_the_category() as $category )
                $post_categories[ $category->term_id ]['posts'][] = $p;
        }
    }

    foreach( $post_categories as $cat ){
        if( ! isset( $cat['posts'] ) || count( $cat['posts'] ) == 0 )
            continue;
        ?>
        <strong><?php echo $cat['cat']->name; ?></strong><br />
        <?php 
        foreach( $cat['posts'] as $post ): 

        ?>
        <a href="<?php echo $post['permalink']; ?>"><?php echo $post['title']; ?></a><br />
        <?php endforeach;
    }

    wp_reset_query();
  • returned this "Warning: Cannot use a scalar value as an array" – Artur Haddad Apr 16 '15 at 13:00
  • thats odd. it doesnt so for me. can you tell me in which line? – websupporter Apr 16 '15 at 13:07
  • i just added $p = array(); before $p['permalink'] = get_the_permalink(); and it works perfect now. thank you sir! – Artur Haddad Apr 16 '15 at 13:10
  • You need to set $p to an array before adding elements to it. – Artur Haddad Apr 16 '15 at 13:11
  • 1
    This can get quite expensive to run. You should look into using transients here. See this post – Pieter Goosen Apr 16 '15 at 13:25

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