• Hi!
    I have three custom post types (about, news, event) and I want to group all those posts under each type on a custom author.php template.
    eg:

    About

    • About 1

    News

    • News 1
    • News 2
    • News 3

    Event

    • Event 1
    • Event 2

    Everything seems to work well, the only problem is that the order of the Groups changes for each user. I want to always have the following order: About, News, Event and not Event, News, About or News, About, Event etc.

    Is there a way to control that order?

    This is my author.php template

    <?php
    /**
     * Custom template for displaying Author Archive pages.
     *
     */
    
    get_header(); ?>
    
    <div class="row collapse">
    	<section id="primary" class="site-content small-12 medium-8 columns">
    		<div role="main">
    
            <?php
                $curauth = (isset($_GET['author_name'])) ? get_user_by('slug', $author_name) : get_userdata(intval($author));
                $author_id = $curauth->ID;
                $post_types = array('about','news','classified');
                $groups = array();
            ?>
    
            <?php $posts_query = new WP_Query(array('post_type'=>array('post','about','news','event'),'orderby'=>'type','order'=>'ASC','author'=>$author_id));?>
    
    		<?php if ($posts_query->have_posts()) : ?>   
    
    			<?php while ($posts_query->have_posts()) : $posts_query->the_post(); ?>
                    <?php $groups[$post->post_type][] = $post;?>
    			<?php endwhile; ?>
    
                <?php foreach ($groups as $group => $posts) : ?>
    
                    <div class="group">
                        <h2><?php echo $group; ?></h2>
                        <ul>
                            <?php foreach ($posts as $post) : setup_postdata($post); ?>
                                <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
                            <?php endforeach ?>
                        </ul>
                    </div>
                <?php endforeach ?>
    
    		<?php else : ?>
    			<?php echo 'No posts!'; ?>
    		<?php endif; ?>
    
    		</div><!-- #content -->
    	</section><!-- #primary -->
    
    	<?php get_sidebar(); ?>
    </div>
    <?php get_footer(); ?>
Viewing 1 replies (of 1 total)
  • Try replacing this:

    <?php foreach ($groups as $group => $posts) : ?>
    
        <div class="group">
           <h2><?php echo $group; ?></h2>
           <ul>
              <?php foreach ($posts as $post) : setup_postdata($post); ?>
                 <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
              <?php endforeach ?>
           </ul>
        </div>
     <?php endforeach ?>

    with this:

    <?php foreach ($post_types as $post_type) : ?>
       <?php $posts = $groups[$post_type]; ?>
    
       <div class="group">
          <h2><?php echo $post_type; ?></h2>
          <ul>
             <?php foreach ($posts as $post) : setup_postdata($post); ?>
                <li><a href="<?php the_permalink() ?>"><?php the_title() ?></a></li>
             <?php  endforeach; ?>
          </ul>
       </div>
    <?php  endforeach; ?>

Viewing 1 replies (of 1 total)
  • The topic ‘WP_Query multiple custom post types and orderby "type"’ is closed to new replies.