don’t use query_posts() – use a custom query with WP_Query() instead;
http://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts
http://codex.ww.wp.xz.cn/Class_Reference/WP_Query
example:
<?php
/**
*Template Name: Single test
*/
?> <div>
<?php if ( have_posts() ): ?>
<?php while (have_posts()) : the_post(); ?><!-- begin of loop for 'event' -->
<p>Event stuff here</p>
<!-- THEN nest in custom post type loop 'speakers': -->
<?php $speakers = new WP_Query( array( 'post_type' => 'speakers' ) ); ?>
<?php if ( $speakers->have_posts() ) : while ( $speakers->have_posts() ) : $speakers->the_post(); ?>
<p>Speaker stuff here</p>
<?php endwhile;
endif;
wp_reset_postdata(); ?>
<!-- THEN resume original events loop: -->
<p>Event stuff here</p>
</article>
<?php endwhile; ?>
<?php endif; ?>
</div>
(untested)
Thanks so much, I tried it but it’s not working. It makes total sense though! Does the first loop have to use WP_Query as well?
possibly remove wp_reset_postdata(); or replace it with $speakers->reset_postdata();
additionally, you could temporarily save $wp_query before the custom loop and restore it after;
example for the custom query and loop:
<?php $temp = $wp_query;
$speakers = new WP_Query( array( 'post_type' => 'speakers' ) ); ?>
<?php if ( $speakers->have_posts() ) : while ( $speakers->have_posts() ) : $speakers->the_post(); ?>
<p>Speaker stuff here</p>
<?php endwhile;
endif;
$speakers->reset_postdata();
$wp_query = $temp; ?>
That seems so perfect!! You are so kind to help! I’m using the code and it’s still not behaving. It’s actually puling in a different custom post type, not speakers, which is odd. Question: I created an archive-speaker.php and a single-speaker.php for the cpt, Is that interfering in some way, should I not include those files since I’m only placing the speakers on the events page?
is the custom post type ‘speaker’ or ‘speakers’?
can you use the pastebin to provide the full code of the template(s)?
http://codex.ww.wp.xz.cn/Forum_Welcome#Posting_Code
what theme are you currently working with?
Hi Alchymyth,
It is ‘speakers’.
I just put the code in pastebin: http://pastebin.com/gCYZBNuw
This is a custom build, using Starkers foundation as a base. It’s very pared down. Do you need the archive-speakers.php or single-speakers.php code?
Note, it uses loop code from the Advanced Custom Fields plugin. Could that be conflicting? Actually, I tried taking that code out, but it didn’t fix it.
Also, how would I get the speakers nav menu to display in alphabetical order?
Do I have too many endifs and endwhiles in there? I’ve tried every combination, taking each out one by one, repositioning them, etc. Should the original query be something other than an ordinary loop?
Yay, I found the missing piece of code. I put this query right before my original loop and voila it all works! Here’s the code for anyone who’s in a jam:
<?php query_posts(‘post_type=events&posts_per_page= -1’); ?>
(you would substitute ‘events’ with whatever your custom post type name is. Use that with the wonderful code that wonderful Alchymyth was so generous to provide and you’re off and running with nested custom loops! Woohoo!
Many thanks again to you, Alchymyth!!!
Hold the phone! I just added a few events and when I click any one of them to get to the main event page, only the last post that was created displays, no others. I tried adding wp_reset_query() at the end of the single-events.php page, but it doesn’t resolve it. Any clues?
Ok, I realized that placing that query code into the single-events.php only made it query the event posts all over again and return the latest 1. Thus, my getting only 1 post (the latest one) on that single page. So I took it out.
And what I’m still left with is figuring out why the nesting of the speakers loop isn’t returning speakers, but is returning a list of my other custom posts and pages, with the styling of the speakers custom post type. Does that shake anything loose?
I found the answer! The ‘speakers’ custom post type declared Has-Archive as True, but it should be FALSE because it’s only being displayed on the events page as a list! So I changed has archive to false and it works!
Which, of course, leads me to the query: How can you nest a loop of a custom post that HAS an archive into another loop? Sigh. This may be a query for another day…
Nonetheless, here is the final code, I hope it can help anyone who’s in a jam:
<?php
/**
*Template Name: Single Events
*/
?>
<?php get_header(); ?>
<div>
<?php if ( have_posts() ): ?>
<?php while (have_posts()) : the_post();?><!-- begin events loop -->
<article>
<!-- Events content here -->
<!-- Then query for Speakers: -->
<?php $temp = $wp_query;
$speakers = new WP_Query( array(
'post_type' => 'speakers',
'paged'=>$paged,
'orderby'=>'title',
'order'=>'ASC'
) ); ?>
<?php if ( $speakers->have_posts() ) : while ( $speakers->have_posts() ) : $speakers->the_post(); ?>
<div class="speakerbio clearfix" id="post-<?php the_ID(); ?>">
<div class="left">
<a href="<?php esc_url( the_permalink() ); ?>"><?php the_post_thumbnail(); ?></a>
</div>
<div class="right">
<h3><?php the_title(); ?> <a href="#top"><i class="fa fa-caret-up"></i></a></h3>
<div class="sptitle"><b><?php the_field('speaker_title'); ?></b></div>
<?php the_field('speaker_bio_text'); ?>
</div>
</div> <!-- END speakerbio -->
<?php endwhile;
endif;
$speakers->reset_postdata();
$wp_query = $temp; ?>
<!-- End speaker query and reset the events post data-->
<!-- Events content here -->
</article>
<?php endwhile; ?>
<?php endif; ?>
</div>
<?php get_footer(); ?>
And, to the moderator who opened up the wonderful world of ‘new WP_Query’ to me. Give a man a fish and he’ll eat for a day. Give a man a fishing pole and he’ll eat for a lifetime. Thanks for the fishing pole, Alchymyth.