I'm creating a custom post type archive page, and I have the posts sorted by category. However, I need the categories to be listed by ID, and the posts to be listed by name.

Here is what I have so far:

//for a given post type, return all
$post_type = 'case-studies';
$tax = 'portfolio';
$tax_terms = get_terms($tax);
if ($tax_terms) {
  foreach ($tax_terms  as $tax_term) {
      'post_type' => $post_type,
      "$tax" => $tax_term->slug,
      'post_status' => 'publish',
      'posts_per_page' => -1,
      'caller_get_posts'=> 1,
      'orderby'=> ID,
      'order'=> ASC

    $my_query = null;
    $my_query = new WP_Query($args);
    if( $my_query->have_posts() ) {
      echo '<h3 class="port">'. $tax_term->name .'</h3><div class="section group">';
        <div class="col span_1_of_4"><div class="box2"><div class="thumbbg"><a href="<?php the_permalink() ?>" rel="bookmark" alt="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?></a></div>
        <h6><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h6></div><!--end box2--></div><!--end col span_1_of_4-->
                                  echo '</div>';

Is it even possible to sort them two different ways?

  • 2
    Please edit your question and add your code directly rather than linking to it on an external site. – Milo Feb 6 '15 at 20:07
  • I have taken the liberty and added your code in your question. :-). Two notes here, don't use external sources to add code because: 1) If the paste is removed, your question would not make sense to future users 2) It is really frustrating paging between multiple pages to read one question. Please keep this in mind in future. Thank you :-) – Pieter Goosen Feb 7 '15 at 7:00

To order taxonomy terms by ID, you have to set the orderby argument:

$tax_terms = get_terms( 'portfolio', array( 'orderby' => 'id' ) );

See the get_terms Codex page for a complete list of arguments for the function.

To order the posts by name (slug), set orderby to 'name' in your query arguments:

$args = array(
    'post_type' => $post_type,
    "$tax" => $tax_term->slug,
    'post_status' => 'publish',
    'posts_per_page' => -1,
    'ignore_sticky_posts'=> 1,
    'orderby'=> 'name',
    'order'=> 'ASC'

Note that 'name' is in quotes, as it is a string. I've also fixed 'ASC' to be a string as well, and changed the deprecated caller_get_posts to ignore_sticky_posts. You should always develop with debugging enabled to be alerted about these errors. Refer to WP_Query in Codex for a full list of valid query arguments and their use.

Your Answer

