0

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

2 Answers 2

1

after some trials I've got one solution that seems to be good but I need some tweaking help

<ul class="list-ensemble">          
<?php
    $terms = get_terms("production_co_type");
    $count = count($terms);
    if ( $count > 0 ){
        echo "<ul>";
        foreach ( $terms as $term ) {
            echo '<li class="title">' . $term->name . '</li>';

            $args = array (
                'post-type'=> 'shows',
                'orderby'=> 'title',
                'order'=> 'ASC',
                'post_per_page'=> -1,
                'production_co_type'=> $term->name
            ); 

            query_posts($args);

            while (have_posts()) : the_post();
                echo get_template_part( 'content', "sort_shows" );
            endwhile;
            wp_reset_query();
        }
        echo "</ul>";
    }
?>
</ul>

It seems everyting works good but the fact that I got only 10 elements "per taxonomy".

I can't understand why. Anyone can help?

Thanks again... I think I'm gonna try something even with the "get_post" function.

Bye Carletto


EDIT

I FINALLY GOT IT!!!

simply add

'nopaging'=> 'true',

to the $args array and got exactly what I'm looking for!

I thank you everyone who helped me. Hope you could give some comment to my solution to improve the quality of my code ;)

Thanks again.

Bye Carletto

0

[addendum] - Sorry, I lost track of the question in the title as I read the description of your problem. I'd still use the array method laid out below for your category example. As far as custom taxonomies go, I'm not sure off the top of my head if there is a better method. It does seem wasteful to create a new WP_Query object for each term. In an ideal world, you would query for all of the posts and sort by the custom taxonomy. [/addendum]

The get_the_category function returns an array of objects, even if only one category is being returned. Your code is close, but needs to treat the returned category as an array.

<?php    $category = get_the_category();
         if ( $category[0]->cat_name != $current_category ) {
             $current_category = $category[0]->cat_name;    ?>

         <li class="category">
             <?php echo $category[0]->cat_name; ?>
         </li>        
<?php    }    ?>

Also of note - if a post is assigned more than one category, only the first will be returned using this method.

1
  • Thanks Jeremy for your answer. Could you please add an example of what I could to with the taxonomy I post in my code. I'd like to get a query of all the post for the "post_type" => "shows" divided into group of production_co_type ("Indepentent studio" has Show1,Show2,Show3 "Major" has Show4,Show5 etc.) And at the top of each group I'd like to have a sort of "title" with the term . Thank you again. Commented Oct 6, 2011 at 23:43

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.