Support

Account

Home Forums General Issues Loop through post content from another post

Solved

Loop through post content from another post

  • Hello,

    So I have some issues getting this thing to work. My scenario:

    I have a post called “Project 1” which is a CPT where I have a post object link which I set to get another CPT post called “Content 1”. Content 1 has a title and a repeater field. “Project 1” has a lot of different fields which works fine but I would like to include the repeater field from “Content 1” on the “Project 1” page. I will also build a filter segment based from the fields in the repeater field from “Project 1”.

    I thought I could use the post object example and return the content from “Content 1” but it’s not working. This is my code:

    <?php $featured_posts = get_sub_field('apartmentlink');
    if( $featured_posts ): ?>
    	<ul>
    		<?php foreach( $featured_posts as $post ): 
    			setup_postdata($post); ?>
          <li>
              <?php the_title(); ?>
              <?php if( have_rows('apartment') ): ?>
      					<?php while( have_rows('apartment') ) : the_row(); ?>
      						<?php the_sub_field('number'); ?>			
      					<?php endwhile; ?>
    				<?php endif; ?>
          </li>
      	<?php endforeach; ?>
        </ul>
    	<?php wp_reset_postdata(); ?>
    <?php endif; ?>

    Problem is that it does only return the title of “Project 1” 24 times and nothing else. What am I doing wrong? I can’t understand why 24 times either.

  • I don’t see any reason, given the code above, why it should not be working.

    However, the fact that it is outputting the title 24 times leads me to think you have a problem outside of this code.

    Is this code inside a have_posts() loop? If yes, how is the query for that loop done? Is it the main WP query or is it a new custom query?

  • Alright, well I’m running roots / sage which shouldn’t make much difference.

    I have a template for the “Project” posts which looks like this:

    {{--
      Template Name: Projekt
    --}}
    
    @extends('layouts.app')
    
    @section('content')
      @while(have_posts()) @php the_post() @endphp
        @include('partials.content-projekt')
      @endwhile
    @endsection
    

    And then I have content-single-projekt page which takes care of the flexible content and it looks like this:

    @include('partials/section-hero')
    
    @include('partials/section-intro')
    
    <?php if( have_rows('content') ): ?>
    	<?php while( have_rows('content') ): the_row(); ?>
        
    		<?php if( get_row_layout() == 'projectfacts' ): ?>
    			<h1>stuff here</h1>
    
    		<?php elseif( get_row_layout() == 'apartmentlayout' ): ?>
    			@include('partials/section-apartment')
    
    		<?php endif; ?>
    	<?php endwhile; ?>
    <?php endif; ?>

    And lastly my section-apartment looks like the example up top.

  • Your issue is that you are using include() to include template parts instead of get_template_part(). When you use include() none of the needed global variables are defined when the included file is run.

  • Alright, I did some more tampering and removed the include() and put the same code there like this:

    <?php elseif( get_row_layout() == 'apartmentlayout' ): ?>
    			<?php $featured_posts = get_sub_field('apartmentlink');
    			if( $featured_posts ): ?>
    				<ul>
    					<?php foreach( $featured_posts as $post ): 
    						setup_postdata($post); ?>
    			      <li>
    			          <?php the_title(); ?>
    			          <?php if( have_rows('apartment') ): ?>
    			  					<?php while( have_rows('repeater_field_name') ) : the_row(); ?>
    			  						<?php the_sub_field('number'); ?>			
    			  					<?php endwhile; ?>
    							<?php endif; ?>
    			      </li>
    			  	<?php endforeach; ?>
    			    </ul>
    				<?php wp_reset_postdata(); ?>
    			<?php endif; ?>
    
    		<?php endif; ?>

    I still get the same issue with 24 titles after eachother with the title from the current post I’m at.

    So I tried this:

    <?php elseif( get_row_layout() == 'apartmentlayout' ): ?>
    			<?php echo '<pre>';
        		print_r( get_sub_field('apartmentlink')  );
    			echo '</pre>';
    			die; ?>
    
    		<?php endif; ?>

    And that on the other hand returns the correct post like this:

    WP_Post Object
    (
        [ID] => 73284
        [post_author] => 11
        [post_date] => 2022-09-19 08:39:19
        [post_date_gmt] => 2022-09-19 06:39:19
        [post_content] => 
        [post_title] => Content 1
        [post_excerpt] => 
        [post_status] => publish
        [comment_status] => closed
        [ping_status] => closed
        [post_password] => 
        [post_name] => content-1
        [to_ping] => 
        [pinged] => 
        [post_modified] => 2022-09-19 08:54:00
        [post_modified_gmt] => 2022-09-19 06:54:00
        [post_content_filtered] => 
        [post_parent] => 0
        [guid] => //localhost:3000/?post_type=apartments&p=73284
        [menu_order] => 0
        [post_type] => apartments
        [post_mime_type] => 
        [comment_count] => 0
        [filter] => raw
    )

    So it doesn’t seem to work even without include

  • Your problem starts all the way back in your main template

    
    @include('partials.content-projekt')
    

    the file loaded here will not have global variables it needs, like $post (the current post) or $wp_query (the main query) along with a host of others.

  • Thank you! I get it now and managed to solve it thanks to you 🙂

Viewing 7 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic.