• Resolved janemig

    (@janemig)


    Hi everybody! Was wondering if anyone would be so kind to help me out. I’m trying to nest my custom post type loop into another one (which is already in its named custom template file). I’m a noob, so not quite sure if my ifs and whiles are in the correct place. The code is below, I stripped out as much as I could for brevity purposes :). The result I’m getting is 1) starts out lovely, looping the original ‘events’ loop, then the ‘speakers’ loop , then 2) takes a turn for the worse with the original ‘events’ loop containing ‘speaker’ content! Any clues? Many thanks in advance for your time and consideration!

    <?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 query_posts('post_type=speakers'); ?>	
    
    					<?php if ( have_posts() ) : while ( have_posts() ) :  the_post(); ?> 
    
    						<p>Speaker stuff here</p>
    
    					<?php endwhile; ?>
    				    <?php endif; ?>
    				    wp_reset_postdata();
    
    				    <!-- THEN resume original events loop: -->
    
    				    <p>Event stuff here</p>
    
            	    </article>
    
    			<?php endwhile; ?>
    			<?php endif; ?>
    
    	 	</div>
Viewing 15 replies - 1 through 15 (of 15 total)
  • don’t use query_posts() – use a custom query with WP_Query() instead;

    https://wordpress.stackexchange.com/questions/1753/when-should-you-use-wp-query-vs-query-posts-vs-get-posts

    https://codex.www.ads-software.com/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)

    Thread Starter janemig

    (@janemig)

    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; ?>
    Thread Starter janemig

    (@janemig)

    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)?

    https://codex.www.ads-software.com/Forum_Welcome#Posting_Code

    what theme are you currently working with?

    Thread Starter janemig

    (@janemig)

    Hi Alchymyth,

    It is ‘speakers’.

    I just put the code in pastebin: https://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?

    Thread Starter janemig

    (@janemig)

    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.

    Thread Starter janemig

    (@janemig)

    Also, how would I get the speakers nav menu to display in alphabetical order?

    Thread Starter janemig

    (@janemig)

    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?

    Thread Starter janemig

    (@janemig)

    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!!!

    Thread Starter janemig

    (@janemig)

    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?

    Thread Starter janemig

    (@janemig)

    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?

    Thread Starter janemig

    (@janemig)

    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!

    Thread Starter janemig

    (@janemig)

    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(); ?>
    Thread Starter janemig

    (@janemig)

    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.

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Nesting Custom Post Type Loops’ is closed to new replies.