Search code examples
phphtmlwordpressarchive

Wordpress: archive multiple post types


I'm trying to figure out if it's possible to archive multiple post types on a page, I have an individual archive for each of the post types working fine, but I also want another page that will archive both of them. I'm still quite new to WP so I'm not at all sure if it's possible but what I'm doing so far isn't working correctly:

    <?php query_posts('post_type=type01'); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <a href="<?php the_permalink(); ?>">
    <div class="type01-div" data-value="<?php
$date = DateTime::createFromFormat('dnY', get_field('type01_date_select'));
echo $date->format('dnY');
?>">STUFF HERE</div>
    </a>

    <?php endwhile; endif; ?>


    <?php query_posts('post_type=type02'); ?>

    <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

    <a href="<?php the_permalink(); ?>">
    <div class="type02-div" data-value="<?php
$date = DateTime::createFromFormat('dnY', get_field('type02_date_select'));
echo $date->format('dnY');
?>">STUFF HERE</div>
    </a>

    <?php endwhile; endif; ?>

So all the posts from 'type01' are showing up, but the posts from 'type02' aren't. Is it possible to archive both? In separate loops though as each post type will be wrapped in a different div class.


Solution

  • You need to reset your query for the next loop, add this between your loops:

    <?php wp_reset_query(); ?>
    

    I have a similar page like this and used this code to do it:

    <h2>type01</h2>
    <?php
    $args = array(
        'post_type' => array( 'type01' ),
        'order' => 'asc',
        'orderby' => 'title',
        'posts_per_page' => -1
    );
    
    $loop = new WP_Query( $args );?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
    
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    
    <?php wp_reset_query(); ?>
    
    </ul>
    
    <h2>type02</h2>
    <ul>
    <?php
    $args = array(
        'post_type' => array( 'type02' ),
        'order' => 'asc',
        'orderby' => 'title',
        'posts_per_page' => -1
    );
    
    $loop = new WP_Query( $args );?>
    <?php while ( $loop->have_posts() ) : $loop->the_post();?>
    
            <li><a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></li>
    
    <?php endwhile; ?>
    

    Check out this link for more info: http://codex.wordpress.org/Function_Reference/wp_reset_query