When a user selects a specific category from a drop-down a list of multiple post types is displayed. I'd like to display the data in groups with the post-type title above in a heading.

I'm unsure how to segment into post-type groups. I spent a bit of time looking but didn't find exactly what I need.

I also tried to add <?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> but it's simply repeating (obviously).

calling the loop

<div id="content" role="main" style="float:right; width:765px;">
        $category_description = category_description();
        if ( ! empty( $category_description ) )
            echo '<div class="archive-meta">' . $category_description . '</div>';
        get_template_part( 'loop', 'support' );

The Loop

<?php else : ?>
<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
    <h2 class="entry-title" style="margin-bottom:3px;"><a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__( 'Permalink to %s', 'twentyten' ), the_title_attribute( 'echo=0' ) ); ?>" rel="bookmark"><?php the_title(); ?></a></h2>
        <span>type = <?php $post_type = get_post_type( $post->ID ); echo $post_type; ?> <?php echo get_post_meta($post->ID, "document-type", true); ?> <?php echo get_post_meta($post->ID, "software-type", true); ?></span>
<?php if ( is_archive() || is_search() ) : // Only display excerpts for archives and search. ?>
        <?php the_excerpt(); ?>
        <span class="%1$s">Compatible with:</span><br />
        <?php if ( count( get_the_category() ) ) : ?>
                <?php printf( __( '%2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-cat-links', get_the_category_list( ', ' ) ); ?>
        <?php endif; ?>
<?php else : ?>
<?php endif; ?>

    <div class="entry-utility">
            $tags_list = get_the_tag_list( '', ', ' );
            if ( $tags_list ):
            <span class="tag-links">
                <?php printf( __( '<span class="%1$s">Tagged</span> %2$s', 'twentyten' ), 'entry-utility-prep entry-utility-prep-tag-links', $tags_list ); ?>
        <?php endif; ?>

Yep, I don't think this is something you can easily do natively.

Two ways that I see:

  1. Use posts_orderby to modify SQL request and order posts by post type, then just need to check so headings don't repeat.

  2. Instead of looping through posts in order returned, take array of those and sort by post type before looping for output.

  • 1
    why not use posts_groupby hook? like answered here wordpress.stackexchange.com/questions/14881/… to this very same question?
    – Bainternet
    Apr 20 '11 at 22:51
  • @Bainternet probably because it's first time I hear about that hook, never used it :)
    – Rarst
    Apr 20 '11 at 23:02
  • great useful hook, but i really wanted to comment on the question and and see why the duplicate question from the same user and i accidentally commented your answer :)
    – Bainternet
    Apr 20 '11 at 23:15
  • Woops, that last question was regarding search, my apologies. However, I did ask about why I wasn't able to get it working in that last post with no response. Apr 21 '11 at 13:46
  • 1
    @NishamMahsin that is mostly pure PHP and not specific to WP development, see array functions in PHP manual for starters.
    – Rarst
    May 26 '14 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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