Sort posts alphabetically by category/custom taxonomy, insert divider between different types

wordpress.stackexchange https://wordpress.stackexchange.com/questions/30419

  •  30-10-2019
  •  | 
  •  

Pergunta

first of all I'd like to thank you for all the great work you're doing. I've found a lot of useful infos and tricks taking a look around in Wordpress Answers.

Well, I'm here to ask so that's my question:

I need to make several "sort by" pages with different criteria.

The first I made was "sort by title" and I use a bunch of code I found here

" Sort posts alphabetically by custom field value, insert divider between different letters "

And I get

A

Post 1 Post 2 Post 3

B

Post 4...

Then I try to edit that code to make a "sort by category" page in which posts are alphabetically ordered but divided into category groups, with a "category title" dividing each group.

So, for example, I'd like to have

Category 1

Post 1 Post 2 Post 3

Category 2

Post 4...

But here is the problem... I'm not so good at php. Anyone can help?

Here's my code

                <ul class="list-ensemble">          
                <?php query_posts(array(
                                      'post_type' => 'shows',
                                      'orderby' => 'title',
                                      'order' => 'ASC',
                                      'posts_per_page'=> -1
                                    ) );  
                                    $current_category = '';
                if ( have_posts() ) while ( have_posts() ) : the_post();
                    $category = get_the_category( $post->ID );
                    if ( $category != $current_category ) {
                        $current_category = $category; ?>
                        <li class="category">
                            <?php echo $category->cat_name; ?>
                        </li>
                    <?php } ?>

                        <?php get_template_part( 'content', "sort_shows" ); ?>

                <?php endwhile; // end of the loop. ?>
</ul>

Thank you guys. Bye

Carletto0282

EDIT

Hi there, as I mention in the title I was trying to made a list of posts sorted even by custom taxonomy. It works like a charm with this code. I just wanna ask if there is a more elegant way to make it work and if I had to add some endwhile somewhere.

Thank you!

                <?php
            $production_co_types = get_terms('production_co_type');
            foreach($production_co_types as $production_co_type) {
              $shows = new WP_Query(array(
                'post_type' => 'shows',
                'orderby' => 'title',
                'order' => 'ASC',
                'post_per_page' => -1,
                'nopaging' => true,
                'taxonomy'=>'production_co_type',
                'term' => $production_co_type->name,
              ));

              echo "<li class='letter'>{$production_co_type->name}</li>";

              while ( $shows->have_posts() ) {
                $shows->the_post();
                $link = get_permalink($post->ID);
                $title = get_the_title();
                echo get_template_part( 'content', "sort_shows" );
              }
            }
             ?> 

I found it here

" Custom taxonomy list page? "

EDIT 2

Ok I'm back again. I'm trying to do what Jeremy says but using taxonomies instead of category (since I see that is more relevant to me this kind of sorting).

Here's the code I made... unfortunately it's not working. Someone can help?

                    <?php query_posts(array(
                                      'post_type' => 'shows',
                                      'orderby' => 'title',
                                      'order' => 'ASC',
                                      'posts_per_page'=> -1,                              
                                    ) );  
                                $current_production_co_type = '';
            if ( have_posts() ) while ( have_posts() ) : the_post();
                $production_co_type = get_the_terms( 'production_co_type', $post->ID );
                if ( $production_co_type->name != $current_production_co_type ) {
                    $current_production_co_type = $production_co_type->name; ?>
                    <li class="letter">
                        <?php echo $production_co_type->name; ?>
                    </li>
                <?php } ?>

                    <?php get_template_part( 'content', "sort_shows" ); ?>

            <?php endwhile; // end of the loop. ?>

Thank you guys Bye

Carletto0282

Nenhuma solução correta

Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange
scroll top