0

I'm trying to get the sub loop of custom post types to display in alphabetical order, but having no luck. I've tried applying ksort and asort to the $posts array, but it just reorders the ID display and isn't doing it for the title.

enter image description here

    // alphabetical menu: https://wordpress.stackexchange.com/questions/119163/displaying-custom-post-type-by-first-letter-through-custom-taxonomy
    // get all totems
    $args = array(
       'post_type' => 'totem',
       'posts_per_page' => -1
    );
    $query = new WP_Query($args);

    // order by first letter of each totem
    $by_letter = array();
    while( $query->have_posts() ) { $query->the_post();
      global $post;
      $letter = substr($post->post_name, 0, 1);
      if ( ! isset($by_letter[$letter]) ) $by_letter[$letter] = array();
      $by_letter[$letter][] = $post;
    }
    wp_reset_postdata();

    // order the array
    ksort($by_letter);

    // fill the array with letters that have no posts
    $by_letter = fill_by_letter_array( $by_letter );

    ?>

    <div class="totem-zoo-alphabet-mobile">
        <ul>
            <?php foreach( $by_letter as $letter => $posts ) {
                    ?>
                    <li class="<?php echo (empty($posts) ? 'empty':'') ?>">
                        <?php echo $letter; ?>
                        <?php if ( ! empty($posts)): ?>
                            <ul>
                                <?php   
                                    ksort($posts); // ATTEMPTING TO SORT POSTS BY TITLE... FAIL
                                    foreach ( $posts as $post ) {
                                    setup_postdata($post);
                                    echo '<li><a href="' . get_permalink() . '">'. get_the_title() . '</a></li>';
                                } ?>
                            </ul>
                        <?php endif;?>
                    </li>
                  <?php
                  }
                  wp_reset_postdata();
            ?>
        </ul>
    </div>

Functions used from here

4
  • 1
    Did you try to just add an orderby to the wp_query? codex.wordpress.org/Class_Reference/… – ChrisL Jun 22 '15 at 8:21
  • yeah, I did add that earlier, but it didn't impact it, I think due to the $by_letter function reordering it... I basically need to ensure that everything is ordered by alphabet, or at least when it's listing the sub ul – Tiger Danky Jun 22 '15 at 8:25
  • 1
    thanks for the slap... as I was typing it in, I realized I needed 'order' => 'ASC' as a reference. thanks, ChrisL – Tiger Danky Jun 22 '15 at 8:28
  • The code is still overly complicated, to my opinion. All that array construction and ksort'ing the letters is not necessary. If your posts are already in alphabetical order, just check the first letter of each post title, and if it changes (store the previous post title's first letter to compare), then print out the letter header. It would also remove the unnecessary setup_postdata() calls, because you could just make all the things in one loop, and then reset post data. – Elmo Allén Jun 22 '15 at 8:50
1

as ChrisL said in the comments, you should add order and orderby to your arguments :

 $args = array(
       'post_type' => 'totem',
       'posts_per_page' => -1,
       'orderby' => 'title',
       'order' => 'ASC'
    );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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