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 it possible to sort posts (custom post type) by category / taxonomy (name, desc)?

For example my categories would be

  • Season 2012
    • some post
    • some post
    • some post
  • Season 2011
    • some post
    • some post
    • some post
  • Season 2010
  • etc..
share|improve this question

2 Answers 2

up vote 3 down vote accepted

One solution would be-

$terms = get_terms('taxonomy-name');
foreach($terms as $term) {
    $posts = get_posts(array(
            'post_type' => 'custom_post_type_name',
            'tax_query' => array(
                array(
                    'taxonomy' => 'taxonomy-name',
                    'field' => 'slug',
                    'terms' => $term->slug
                )
            ),
            'numberposts' => -1
        ));
    foreach($posts as $post) {
        // do what you want to do with the posts here
    }
}
share|improve this answer
    
Great! Many thanks –  Iladarsda Oct 15 '12 at 18:42
    
Good answer, though you should watch your indentation ;) –  kovshenin Oct 15 '12 at 18:59
    
You are right, i'll be more careful in the future. Edited the answer above –  Mridul Aggarwal Oct 15 '12 at 19:07
    
IS there any way to order foreach $term->name, DESC? –  Iladarsda Oct 15 '12 at 19:11
    
yes there is. Change first line to- $terms = get_terms('taxonomy-name', array('order' => 'DESC')); Check this codex page for full reference- codex.wordpress.org/Function_Reference/get_terms –  Mridul Aggarwal Oct 15 '12 at 19:17

Alternative loop, where tax = category and custom post type = "results"

$args = array( 'taxonomy' => 'category' );
$terms = get_terms( 'category', $args);

//START FOR EACH LOOP
foreach ($terms as $term) {

            $post_type = 'results';
            $tax = 'category';
            $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,
                    'caller_get_posts'=> 1
                );



                $my_query = null;
                $my_query = new WP_Query($args);

                if( $my_query->have_posts() ) : ?>

                        // WHILE
                        <?php while ( $my_query->have_posts() ) : $my_query->the_post(); ?>

                        // Do stuff

                        <?php endwhile; ?>
                        // END WHILE


                <?php else : ?>
    <?php endif; wp_reset_query(); ?>

}
// END FOR EACH LOOP
share|improve this answer
    
"caller_get_posts" is deprecated BTW. Use "ignore_sticky_posts" Source - codex.wordpress.org/Class_Reference/… –  Mridul Aggarwal Oct 15 '12 at 19:02

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.