Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there a way I can list all posts in a specific custom post type and arrange them by the custom taxonomy term attached to them?

For example;

Taxonmy Term #1
Post Type
Post Type
Post Type

Taxonomy Term #2
Post Type
Posty Type

Any help would be most appreciated.

Thanks.

share|improve this question

4 Answers 4

up vote 9 down vote accepted

Try this

$custom_terms = get_terms('custom_taxonomy');

foreach($custom_terms as $custom_term) {
    wp_reset_query();
    $args = array('post_type' => 'custom_post_type',
        'tax_query' => array(
            array(
                'taxonomy' => 'custom_taxonomy',
                'field' => 'slug',
                'terms' => $custom_term->slug,
            ),
        ),
     );

     $loop = new WP_Query($args);
     if($loop->have_posts()) {
        echo '<h2>'.$custom_term->name.'</h2>';

        while($loop->have_posts()) : $loop->the_post();
            echo '<a href="'.get_permalink().'">'.get_the_title().'</a>';
        endwhile;
     }
}

We get all the terms of a taxonomy, loop through them, and fire off a title link to each post that belongs to that term. If you need to reorder the taxonomy terms, you can do so with a plugin pretty easily. Reorder Taxonomy, I believe. But pay attention that this plugin adds(!) another column to your table on activation and does not remove it upon deactivation!

share|improve this answer
    
Perfect. Thanks for that! –  Dean Elliott Sep 25 '12 at 17:40
    
The more elegant solution I was looking for. Nice job! –  theMojoWill Sep 28 '12 at 8:43
    
Thanks @theMojoWill ! –  GhostToast Sep 28 '12 at 14:29

Not a particularly elegant solution but you can create multiple queries each for the specific terms and then output them. Hopefully someone can come up with a nicer way of automatically pulling the terms to modify the output/sorting. But this would get you going.

<?php

//First Query for Posts matching term1
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'taxonomy_1',
            'field' => 'slug',
            'terms' => array( 'term1' )
        ),
    ),
    'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );

if ( have_posts() ) {

    $term = $wp_query->queried_object;

    echo 'All posts found in ' . $term->name;

    while ( have_posts() ) : the_post();
        //Output what you want
        the_title();
        the_content();
    endwhile;
}

//RESET YOUR QUERY VARS
wp_reset_query();

//Second Query for term2
$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'taxonomy_1',
            'field' => 'slug',
            'terms' => array( 'term2' )
        ),
    ),
    'post_type' => 'my-post-type'
);
$query = new WP_Query( $args );

if ( have_posts() ) {

    $term = $wp_query->queried_object;

    echo 'All posts found in ' . $term->name;

    while ( have_posts() ) : the_post();
        //Output what you want
        the_title();
        the_content();
    endwhile;
}
share|improve this answer

Nice one! GhostOne's solution was what I had been looking for. In my situation the custom post type was 'minining_accidents' and custom taxonomies associated with this was 'accident-types' which had multiple terms under it. My idea was to create a custom widget to show list of posts under terms in this custom taxonomies. In my trial run it got what I wanted. Rest was spruce up. Here is my code:

function fn_get_list_of_mining_accident_types()
{
    $custom_taxonomy='accident-types';  
    $custom_terms = get_terms($custom_taxonomy);    
    $str_return='<ul>';
    foreach($custom_terms as $custom_term) 
    {
        wp_reset_query();
        $args = array(
            'post_type' => 'minining_accidents',
            'tax_query' => array(               
                array(
                    'taxonomy' => $custom_taxonomy,
                    'field' => 'slug',
                    'terms' => $custom_term->slug,
                ),
            ),
        );  

        $loop = new WP_Query($args);

        $term_name=$custom_term->name;
        $term_slug=$custom_term->slug;
        $term_link=get_term_link($term_slug, $custom_taxonomy);

        $str_return.='<li><a href="'.$term_link.'">'.$term_name.'</a>';

        if($loop->have_posts()) 
        {
            $str_return.='<ol>';

            while($loop->have_posts()) : $loop->the_post();
                $str_return.='<li><a href="'.get_permalink().'">'.get_the_title().'</a></li> ';
            endwhile;

            $str_return.='</ol>';           
         }
         $str_return.='</li>';
    }
    $str_return.='</ul>';
    return $str_return;
}

Yes! There always is an option to further improve the code.

share|improve this answer

To show a list of custom posts from a custom taxonomy

$args = array(
    'tax_query' => array(
        array(
            'taxonomy' => 'your-custom-taxonomy',
            'field' => 'slug',
            'terms' => array( 'your-term' )
        ),
    ),
    'post_type' => 'your-post-type'
);
$loop = new WP_Query($args);
     if($loop->have_posts()) {
    $term = $wp_query->queried_object;
     while($loop->have_posts()) : $loop->the_post();
        //Output what you want      
   echo '<li><a href="'.get_permalink().'">'.get_the_title().'</a></li>';
      endwhile;
}

Title

  • List item
  • List item
  • List item
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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