Support

Account

Home Forums Front-end Issues Extracting Post Object data from within a Repeater

Solved

Extracting Post Object data from within a Repeater

  • As you can see from the screenshot linked to below…

    https://www.evernote.com/shard/s97/sh/7bd49b3c-3ec2-41c8-aedb-c56eda3f1dad/984d978647e2102fbeecb29f953b5ae6

    …we have a post object select menu nested within a repeater field. I cannot seem to wrap my head around how I should be setting this up in the page template. Any help is greatly appreciated.

    Rich

  • What do you want to display? You can just use the post object as is like this:

    
    <?php
    $postobject = get_sub_field('nameofyourpostobjectdropdownfield');
    $postobject->post_title //post title
    $postURL = get_permalink($postobject->ID); //URL
    apply_filters('the_content', $postobject->post_content); //The post content formatted
    $postdate->post_date //post publish date
    ?>
    
    

    And there’s more if you do a print_r($postobject); and take a look 🙂

  • Thanks for the quick response. This appears to be what I was looking for…turns out I forgot to use “sub” in get_sub_field, like an idiot

    Rich

  • Haha been there!

    Good thing it worked out 🙂

    /Jonathan

  • Hey Jonathan –

    I gave that a shot but it isn’t working, and I think it’s because I am trying to access this data from within a foreach loop, like so:

    <div class="news-container">
    
    <?php $news = get_field('ssm_featured_content'); ?>
    
    <?php foreach ($news as $item) { ?>
    
    	<?php $postObject = get_sub_field('ssm_featured_page'); ?>
    	
    	<?php echo $postObject->$post_title; ?>
    	
    	
    <?php } ?>
    
    </div>
    <!-- end .news-container -->

    I tried to adjust as follows, but I am not sure how the code should look:

    <?php echo $item[$postObject->$post_title]; ?>

    but that didn’t seem to be correct either. I’m just not sure how to handle this.

    Rich

  • what is ssm_featured_content? your repeaterfield?

    you’ll need to change it to:

    
    <?php while (has_sub_field('ssm_featured_content')) { ?>
    
    	<?php $postObject = get_sub_field('ssm_featured_page'); ?>
    	
    	<?php echo $postObject->$post_title; ?>
    	
    	
    <?php } ?>
    
    
  • Yes, ssm_featured_content is my repeater field.

    The code above returns:

    Array

    Array

    There are two items in the repeater, so two arrays are being returned.

    Thanks for helping me through this, and sorry if this is becoming a PITA for you.

  • that does not matter with the code i provided.. what it’ll do is loop through all repeaters and fetch the ssm_featured_page dropdowns value. But I guess that is an array then so you’ll do this (if you want it foolproof for multiple selects:

    
    
    <?php while (has_sub_field('ssm_featured_content')) { ?>
    
    	$postObjects = get_sub_field('ssm_featured_page');
    
    if($postObjects){
    foreach($postObjects as $post){
    setup_postdata($post);
    the_title();
    }
    wp_reset_postdata();
    }
    	
    	<?php echo $postObject->$post_title; ?>
    	
    	
    <?php } ?>
    
    
  • i had a typo in my code from your earlier response. it was correct! Thanks. Rich

  • Great 🙂

    Glad to help out!

  • Hi guys,

    Just wanted to say that this post helped me out a lot. I reference you in this post over here. I hesitate to recommend my route to others as I’m not sure it’s the most efficient way to get the desired result… but it does get the result I was looking for.

    Thanks

    My post about trying to get relationship-related post’s or post_object’s fields out of repeater fields.

  • this post has saved my life (especially my time) too !

    Thanks a lot 🙂

  • Hey guys, this looks like the data I’m after, but I don’t get too deep into programming. If one of you could clarify, I’d really appreciate it. Here’s my situation:

    I have a repeater field called favorite_cars. In this field, I have a post object field called select_car. This is linking to a custom post type I have with it’s own set of ACF fields. I want to access those. I can with a normal post object chunk of code, but don’t know how to do it within a repeater field.

    So again, to summarize:

    Repeater: favorite_cars
    Post Object Field: select_car
    Custom Post Type used in Post Object filter: carsandtrucks
    ACF field in the carandtrucks CPT: main_image

    So I’m trying to pull say the picture of a Corvette (main_image) that’s in the carsandtrucks CPT from a select post object field named select_car in the repeater field called favorite_cars.

    Hope that makes sense. Thank you!

  • Hi!

    If I understand you correctly this should do it

    
    
    <?php 
    	//Loops through the repeater
    while (have_rows('favorite_cars')) {
    	the_row();
    	
    	//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
    	$car_object = get_sub_field('select_car');
    	if($car_object){
    		//Fetch the image field from the carsandtrucks post
    		$image = get_field('main_image', $car_object->ID);
    	}
    	
    	//Echo out the image with the medium size. Change this as you like!
    	echo '<img src="' . $image['sizes']['medium'] . '" width="' . $image['sizes']['medium-width'] . '" height="' . $image['sizes']['medium-height'] . '" alt="' . $image['alt'] . '" />';
    	
    	
    } 
    ?>
    
    
  • Holy crap Jonathan, that worked. Thanks man!!!!

    I had to change the image line to make it work, I guess because my site doesn’t have medium images?

    Here’s what I have:

    <img src="' . $image . '" width="' . $image . '" height="' . $image . '" alt="' . $image['alt'] . '" />

    Is that bad? Not sure how it’s generating the alt tag though.

  • One more question, and I’m happy to pay you for time, just shoot me your PayPal:

    So the above works beautifully. Now what if I wanted to also pull a field or fields from the existing repeater field, meaning not the post object. I’m trying this, but it’s not working:

    <?php 
    	//Loops through the repeater
    while (have_rows('favorite_cars')) {
    	the_row();
    	
    	//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
    	$car_object = get_sub_field('select_car');
            $title = get_sub_field('car_title');
            $summary = get_sub_field('car_summary');
    	if($car_object){
    		//Fetch the image field from the carsandtrucks post
    		$image = get_field('main_image', $car_object->ID);
    	}
    	
    	//Echo out the image with the medium size. Change this as you like!
    	echo '<img src="' . $image . '" width="' . $image . '" height="' . $image . '" alt="' . $image['alt'] . '" />';
            echo '<div>$title</div>';
            echo '<div>$summary</div>';
    	
    } 
    ?>

    So as you can see, I’m trying to pull a title and summary (both ACF fields) from the current post, along with the post object from the other.

    In case this doesn’t make sense, someone is choosing a car from another CPT, and then entering their own title and descriptions on the current post.

    Thank you!

  • Hi!

    You’ve probably set the return value for the image field to be URL rather than image object. So you should not use that markup for the image. Here’s a complete version of what you’re doing:

    
    <?php 
    //check for the repeater first. We dont want to do a while loop if there are none!
    if(have_rows('favorite_cars')){
    	
    	//Loops through the repeater
    	while (have_rows('favorite_cars')) {
    		the_row();
    		
    		//fetches the post object field. Assumes this is a single value, if they can select more than one you have to do a foreach loop here like the one in the example above
    		$car_object = get_sub_field('select_car');
    	    $title = get_sub_field('car_title');
    	    $summary = get_sub_field('car_summary');
    		if($car_object){
    			//Fetch the image field from the carsandtrucks post
    			$image = get_field('main_image', $car_object->ID);
    		}
    		
    		//Echo out the image. Since you set it to only return the image URL you can only use this for src. Change the field settings to object if you want to use the version i previously posted. 
    		echo '<img src="' . $image . '" />';
    	    echo '<h2 class="favorite-car-title"><' . $title . '</div>';
    	    //Depending on what kind of field the summary is you want the <p> tag around it.. If the field is a wysiwyg field or a textarea with automatic p-tags, remove the <p> and </p> from the code. 
    	    echo '<div class="favorite-car-summary"><p>' . $summary . '</p></div>';
    		
    	}	
    	
    } 
    ?>
    
    

    Its such a small thing so don’t worry about compensation!

  • Jonathan, dude…I owe you. If you’re ever in Santa Monica, CA look me up and remind me that I owe you dinner.

    Your code worked great, and did exactly what I needed. I would never have been able to figure that out.

    Thanks man!!!

  • Np dude, just giving some back…

    It’s a long trip from Sweden but I’ll keep that in mind 😉

  • Lol. Well aren’t I the typical American, assuming you’re here like that.

    Don’t come out this way, I’d rather bring my wife there for a trip!

    Thanks again man. If you do contract work, send me your info. I might need someone who knows more in the future.

  • Hehe I get that 🙂

    Sweden is probably an interesting country to visit as an american. Lots of cultural differences (not to say the weather).

    I work for a company called tigerton so any contract work would be going through there. Should you need me you can always send me an email on [email protected]

    Have a great sunday (I think yours is about to start now)

  • Hi i’d like to ask why is that there’s this phrase “hello world!” that concatenates to the title of my post? Thank you. What could be the reason?
    Here’s my code:

    <?php while (has_sub_field('post_slideshow')) { ?>
    
    	<?php $postObjects = get_sub_field('slide');
    	if($postObjects){
    		foreach($postObjects as $post){
    		setup_postdata($post);
    		the_post_thumbnail();
    		the_title();
    		}
    	wp_reset_postdata();
    	}
    ?>
    	
    	<?php echo $postObject->$post_title; ?>
    	
    	
    <?php } ?>
  • i know this is an older post but I can’t seem to get this worked out.
    I have a custom post type “event” that has a repeater field in it. One of the repeater sub fields is a relational post object field to another custom post type that i need pull data from a custom field.

    So here is what i have. Event has a field called event_session which is a repeater field. One of the sub-fields is called “session_speaker” it is a post object that is connected to my speaker custom postype.

    When someone creates a row from the repeater field in my event page they will choose a speaker. On the front end i want to pull that speakers company name and twitter link (both custom fields).

    Here is what i tried:

    <?php
    
        $args = array(
            'post_type' => 'event'
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
    <!-- WP_Query WordPress loop -->
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
        <?php if(get_field('event_session')): ?>
            <?php while(has_sub_field('event_session')): ?>
                    <strong><?php the_sub_field('session_start'); ?>  </strong><?php the_sub_field('session_title'); ?><br>
    <?php
    $speakers = get_sub_field("session_speaker");
    if ($speakers && count($speakers)>0)
    {
        foreach ($speakers as $speaker)
        {
            echo $speaker->ID; //$speaker is a post object//
        }        
    }
    ?>	
    
            <?php endwhile; ?>
        <?php endif; ?>
    
    <?php endwhile; else: ?>
    
        <!-- Displayed if no posts or pages are available -->
        <p>There are no posts or pages here!</p>
    
    <?php endif; ?>

    This shows nothing, but if i change the echo $speaker->ID to just echo $speaker, the array data does show up.

    I tried from above as well:

    <?php
    
        $args = array(
            'post_type' => 'event'
        );
    
        $the_query = new WP_Query( $args );
    ?>
    
    <!-- WP_Query WordPress loop -->
    <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>
    
        <?php if(get_field('event_session')): ?>
            <?php while(has_sub_field('event_session')): ?>
                    <strong><?php the_sub_field('session_start'); ?>  </strong><?php the_sub_field('session_title'); ?><br>
    
    <?php	$postObjects = get_sub_field('session_speaker');
    
    if($postObjects){
    foreach($postObjects as $post){
    setup_postdata($post);
    the_title();
    }
    wp_reset_postdata();
    }?>
    	
    	<?php echo $postObject->$post_title; ?>
    
            <?php endwhile; ?>
        <?php endif; ?>
    
    <?php endwhile; else: ?>
    
        <!-- Displayed if no posts or pages are available -->
        <p>There are no posts or pages here!</p>
    
    <?php endif; ?>	

    This shows the speaker title but even though I have 4 rows in my repeater field it will only show the first one.

  • Hi Jonathon
    I know I am very late on this.. But I have used your code in my repeater to get the post object repeated ..

    I was trying like this

    ` <?php $postobject = get_sub_field(‘post_repeater’);?>
    <div class=”col-sm-3 product-item”>

    <a href=”<?php $postURL = get_permalink($postobject->ID); ?>”>

    <img class=”product-img” src=”<?php //bloginfo(‘template_directory’);?>/images/product-images/321600_moelleux_blanc_trepais.png” width=”100%” />

    <p class=”product-name”><?php $postobject->post_title; ?></p>
    <p class=”product-seemore”>See More</p>
    </a>

    </div><!– col ends here –>`

    But My repeater just repeat row and doesn’t produce Post object..

    I am using this repeater in a page template and repeating it in a Page

    Here is My ACF field settings

  • Hi Guys
    I am trying to achieve like this.. I am trying to show a post object as repeater inside 2 nested repeater field.. I am using this repeater and post object in a page template called page-bagels.php . These 2 repeater showing the 2 row as output as expected. But the post object is not showing or not returning value. Rather it is returning value of the page. Like where I have used <?php the_title(); ?> it is returning the page title and not returning the post object title. My code is as below

    
      <?php while ( have_posts() ) : the_post(); ?>        
    		<div class="main-content">    	
    			<div class="main-wrap">                	            
    				<div class="col-sm-12">                
    					<div class="breadcrumbs">                    
    						<?php if(function_exists('bcn_display'))                {                    bcn_display();                }?>                  
    					</div>  <!-- breadcrambs ends here -->                              
    				</div><!-- col-sm-12 ends here -->                                    
    				<?php 				// check for rows (parent repeater)				
    				if( have_rows('product_page') ): ?>                        
    				
    				<div class="col-lg-12 row product-row">                        	
    					<?php 					
    					// loop through rows (parent repeater)					
    					while( have_rows('product_page') ): the_row(); ?>                            
    					<div class="col-sm-12">                            
    						<div class="marque">                                
    							<span class="brand-lg"><img src="<?php the_sub_field('brand_image'); ?>" /></span>                            </div>                        
    					</div>                                                
    					<?php 	
    					// check for rows (sub repeater)							
    					if( have_rows('post_repeater') ): ?>                                                                                                                        <div class="col-sm-12 product-item-row">                               
    					<?php 						
    					// loop through rows (sub repeater)					
    					while( have_rows('post_repeater') ): the_row();?>                                
    					<div class="col-sm-3 product-item">                	                                        
    					<?php	$postobject = get_sub_field('product_post'); ?>      
    						<a href="<?php $postURL = get_permalink($postobject->ID);?>">                        
    						<img class="product-img" src="<?php bloginfo('template_directory');?>/images/product-images/321600_moelleux_blanc_trepais.png" width="100%" />                        
    						<p class="product-name"><?php the_title();?></p>                        
    					<p class="product-seemore">See More</p>                    		</a>                                                                       
    					</div><!-- col ends here -->                                 
    					<?php endwhile; ?>                                
    					</div><!-- col Product Items row here -->                                
    					<?php endif; //if( get_sub_field('items') ): ?>                                
    					<?php endwhile; // while( has_sub_field('to-do_lists') ): ?>                        
    				</div><!-- col lg 12 ends here or row ends here -->                                  	
    				<?php endif; // if( get_field('to-do_lists') ): ?>			
    				<?php endwhile; // end of the loop. ?>                  
    				
    					</div><!-- main wrap ends here -->               
    				<div class="clr"></div>    
    		</div><!-- main content ends here -->
    
    

    So in this code you can see the post object is also a repeater.. So it means I am using 3 repeaters. If you see the code you will get it

    Here is the output I am getting http://ahsanurrahman.com/myprojects/cms/wp/gadoua/en/bagels/

    So any help is appreciated.

Viewing 25 posts - 1 through 25 (of 26 total)

The topic ‘Extracting Post Object data from within a Repeater’ is closed to new replies.