WordPress.org

Ready to get started?Download WordPress

Forums

Display Custom Posts by Term in Hierarchy (2 posts)

  1. drummergirl
    Member
    Posted 2 years ago #

    I thought this was a simple concept but I've spent nearly 5 hours scouring the Interwebs and I can't find what I need.

    I have a custom post type called "gifts" and a taxonomy called "relationship". The taxonomy has a hierarchy structure.

    I want to spit out a navigation for this taxonomy that uses the accordion jQuery effect. I can achieve this by hard coding the links in so they jQuery is working properly.

    Since I have hundreds of custom posts (and expect it to be into the thousands, soon) I'd like to make the menu populate automatically. What I need it to do is display all of my taxonomy terms in hierarchical order and list the posts associated with each term underneath it.

    It seems I can get the terms with wp_list_categories, but not the posts. I've tried about a dozen or more other pieces of code, but they all return a flat list of terms in alphabetical order. Seems I can't marry the two options.

    Is this type of a query even possible?

  2. drummergirl
    Member
    Posted 2 years ago #

    For the record, this actually spits out what I need, but the terms are sorted alphabetically and not in a hierarchy like I have set up in the dashboard.

    <?php
    //for a given post type, return all taxonomies attached to it
    $post_type = 'gifts';
    $tax = 'relationship';
    $hierarchical = 1;
    $tax_terms = get_terms($tax);
    if ($tax_terms) {
      foreach ($tax_terms  as $tax_term) {
        $args=array(
          'post_type' => $post_type,
          "$tax" => $tax_term->slug,
          'post_status' => 'publish',
          'posts_per_page' => -1,
          'hierarchical' => $hierarchical,
          'caller_get_posts'=> 1
        );
    
        $my_query = null;
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo ' <h3>'.$tax_term->name.'</h3>';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <a href="<?php the_permalink();?>"><?php the_title();?></a>
            <?php       endwhile;     }     wp_reset_query();   } } ?>

Topic Closed

This topic has been closed to new replies.

About this Topic