Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using the code below to sort by name based on taxonomy and then list the posts within each taxonomy.

First query works ok for alphabetical listing of taxonomies.

The problem i am having is that the posts inside each taxonomy are not listed in alphabetical order. Any ideas?

<ul style="margin: 10px 25px 45px 25px !important;">
        <?php if (have_posts()) : while (have_posts()) : the_post(); ?>            

            <?php 

            //if (false == ($content = get_transient('os_travel_cache'))){

            //  ob_start();

                $taxonomies = array(
                    'tax_one'       => 'Taxonomy1',
                    'tax_two'       => 'Taxonomy2',
                    'tax_three' => 'Taxonomy3',
                );


                foreach($taxonomies as $taxonomy => $title) { 
                    // start div... cor column..
                    ?>
                    <li>
                        <div><h6><?php echo $title; ?></h6></div>
                        <?php 

                            $terms = get_terms( $taxonomy, 
                                array(
                                    'orderby'       => 'name', 
                                    'order'         => 'ASC',
                                    'hide_empty'    => true ) ); 

                            if (!is_wp_error($terms) && is_array($terms) && count($terms) > 0){
                                foreach($terms as $term){

                                    $args = array(
                                        'post_type' => 'os_book',
                                        'tax_query' => array(
                                            array(
                                                'taxonomy' =>  $term->taxonomy,
                                                'field' => 'id',
                                                'terms' => $term->term_id
                                            )
                                        )
                                    );

                                    ?>
                                    <div class="term_container">
                                        <div class="handler"><span id="ashplus"> + </span><?php echo $term->name; ?></div>
                                        <div>
                                            <ul class="list">
                                    <?php

                                    $rp = new WP_Query( $args );
                                    if ($rp->have_posts())
                                        while ( $rp->have_posts() ) { 
                                            $rp->the_post(); 

                                                $name       = get_field('fl_profile_name');
                                                $location   = get_post_meta(get_the_id(), 'fl_country', true);

                                            ?>
                                                <li data-location="<?php echo $location; ?>"><a href="<?php the_permalink();?>"><?php echo $name; ?></a></li>

                                            <?php 
                                        }
                                    ?>
                                            </ul>
                                        </div>
                                    </div>
                                    <?php
                                }
                            }

                        ?>
                    </li>
                    <?php
                }

        //      $content = ob_get_contents();

        //      ob_end_clean(); 
        //      set_transient('os_travel_cache', $content, 1);
        //  }
        //  echo $content;
            ?>


        <?php endwhile;
        endif; ?>

        </ul>
share|improve this question

2 Answers 2

up vote 1 down vote accepted

Posts are not sorted alphabetically, because you don't tell WordPress to sort them in such way. By default they will be sorted by date in descendant order (newest first).

What you should do, is change your WP_Query args by adding order and orderby, so it should look like this:

$args = array(
    'post_type' => 'os_book',
    'orderby' => 'title',
    'order' => 'ASC',
    'tax_query' => array(
        array(
            'taxonomy' =>  $term->taxonomy,
            'field' => 'id',
            'terms' => $term->term_id
        )
    )
);

PS. You should sort them by title and not by name, I guess. Results of these 2 methods can be different and sorting by name won't give you correct alphabetical order in every case (i.e. it will ignore accents).

share|improve this answer

I am trying to sort custom posts by taxonomy using the code below, but it is not working. It displays results but just sorts in the order I entered them in the database. Would really appreciate any help. I am not a coder BTW.

    $args = array(
        'post_type' => 'staff',
        'order_by'  => 'title',
        'order' => 'asc',
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'department',
                'field' => 'slug',
                'terms' => 'staff'
            )
        )
    );
share|improve this answer
1  
Had cut and pasted above from somewhere. Turns out it should be 'orderby' not 'order_by'. Fixed that and it worked. Doh! –  user49521 Mar 26 at 15:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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