Skip to content

Instantly share code, notes, and snippets.

@rachelmccollin
Last active September 9, 2021 17:28
Show Gist options
  • Save rachelmccollin/93772b5f9a86936aebe4d03b530c3d89 to your computer and use it in GitHub Desktop.
Save rachelmccollin/93772b5f9a86936aebe4d03b530c3d89 to your computer and use it in GitHub Desktop.
WPMU DEV One Query Multiple Loops
<?php while ( $query->have_posts() ) : $query->the_post();
if ( get_post_type( $post->ID ) == 'book' && $count == 0 ) {
include( locate_template( 'template-parts/loop-frontpage.php' ));
$count++;
}
endwhile; // end first loop for books
rewind_posts();
while ( $query->have_posts() ) : $query->the_post(); // start fourth loop for book club
if ( get_post_type( $post->ID ) == 'page' && $post->post_name == 'bookclub' && $count == 3 ) {
include(locate_template( 'template-parts/loop-frontpage.php' ));
$count++;
}
endwhile; // end fourth loop for page
rewind_posts();
?>
include(locate_template( 'template-parts/loop-frontpage.php' ));
$count = 0;
?>
<section class="blog-posts grid">
</section>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'quarter left' ); ?>>
while ( $query->have_posts() ) : $query->the_post(); // start second loop for wordpress posts
if ( get_post_type( $post->ID ) == 'post' && in_category( 'wordpress' ) && $count == 1 ) {
include(locate_template( 'template-parts/loop-frontpage.php' ));
$count++;
}
endwhile; // end second loop for wordpress
rewind_posts();
.quarter {
width: 23.5%;
margin: 0 0.5%
}
.left {
float: left;
}
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php if ( $count == 0 ) {
$title = 'Books';
}
elseif ( $count == 1 ) {
$title = 'WordPress';
}
elseif ( $count == 2 ) {
$title = 'News';
}
elseif ( $count == 3 ) {
$title = 'Book Club';
}
?>
<h2><?php echo $title;?></h2>
<?php if ( has_post_thumbnail() ) { ?>
<a href="<?php the_permalink(); ?>">
<?php
the_post_thumbnail( 'grid' );
?>
</a>
<?php } ?>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<?php the_excerpt(); ?>
</article>
while ( $query->have_posts() ) : $query->the_post(); // start third loop for news posts
if ( get_post_type( $post->ID ) == 'post' && in_category( 'news' ) && !in_category( 'wordpress' ) && $count == 2 ) {
include(locate_template( 'template-parts/loop-frontpage.php' ));
$count++;
}
endwhile; // end third loop for news
rewind_posts();
$args = array(
'post_type' => array ( 'book', 'post', 'page' )
);
$query = new WP_query ( $args );
if ( $query->have_posts() ) {
}
@djmtype
Copy link

djmtype commented Sep 9, 2021

Can you put this altogether in order? I tried following the post – https://wpmudev.com/blog/how-to-use-one-query-to-run-multiple-loops/
However, my loop still seems to be stuck.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment