WordPress.org

Ready to get started?Download WordPress

Forums

Sort Custom Post Type entries by 2 custom taxonomies (2 posts)

  1. unwrittendevin
    Member
    Posted 8 months ago #

    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!

  2. Howdy_McGee
    Member
    Posted 8 months ago #

    I can't say I know the answer to your problem, but it seems like you would need to relate the taxonomies somehow (or at least 2 of them). Here's what I found which might be of some help:

    WP Stack Exchange - Relating 2 Taxonomies

    WP Codex - Taxonomy Parameters

Reply

You must log in to post.

About this Topic