WordPress.org

Support

Support » Themes and Templates » Sort Custom Post Type entries by 2 custom taxonomies

Sort Custom Post Type entries by 2 custom taxonomies

  • unwrittendevin
    Member

    @unwrittendevin

    Hi All,

    I’m trying to sort a list of publications by type, year and month. I have my query looking first into the “type” custom taxonomy called “document” to list the different publication types. But after getting it to organize my posts by the type, I’d like to then list them chronologically by year (2012 -> 1998) and then by time of year (Spring -> Winter)

    It would look like this:
    Magazines:
    2012

    • Spring
    • Summer
    • Autumn
    • Winter

    2011

    • Spring
    • Summer
    • Autumn
    • Winter

    … and so on

    My Custom Taxonomy Names
    Type = document (magazines, annual reports)
    Year = pub_year (2012 -> 1998)
    Time = pub_time (Spring, Summer, Autumn, Winter)

    Here is my code:

    $pdf_files = get_terms('document');
      foreach ( $pdf_files as $pdf_file ) {
        echo '<h2 class="clearfix" id="'.$staff_role->slug.'">';
        echo $pdf_file->name;
        echo '</h2>';
        if ($pdf_file->description):
          echo '<p>';
          echo $pdf_file->description;
          echo '</p>';
        endif;
        $years = get_terms('pub_year');
        $query = new WP_Query(
        $args = array(
          'posts_per_page' => '-1',
          'document' => $pdf_file->slug,
          'pub_year' => $years->slug,
          'order' => 'ASC',
        ) );
        while ( $query->have_posts() ) : $query->the_post();
          $pdf_times = get_the_terms( $id, 'pub_time');
          foreach ( $pdf_times as $pdf_time ) {
            $pubtime = $pdf_time->name;
          }
          $pdf_years = get_the_terms( $id, 'pub_year');
          foreach ( $pdf_years as $pdf_year ) {
            $pubyear = $pdf_year->name;
          }
          echo $pubyear;
          if ($pubtime != ''){
            echo " - ";
            echo $pubtime;
          }
          echo '<div class="row-fluid pad-one-b clearfix">';
          get_template_part('column', 'pubs');
          echo '</div>';
          $pubyear = "";
          $pubtime = "";
        endwhile;
        wp_reset_postdata();
        $query = null;
    }

    Any help would be greatly appreciated!

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Sort Custom Post Type entries by 2 custom taxonomies’ is closed to new replies.