Support

Account

Home Forums Add-ons Repeater Field Using repeaters and post object fields together

Solving

Using repeaters and post object fields together

  • Apologies if this is a repeat of an existing question – I have found at least one similar thread but it’s not fixed the issue I’m having. I suspect it’s a basic error on my part but there are so many things nested here that I just can’t see it.

    In brief: I have a site (very much in dev) which lists authors as one Custom Post Type and books as another. I need to be able to create links between these and am using post object fields to do so. All book posts have a single PO field (author) to designate their author post; all author posts have a repeater field (books) containing a single PO (title) so as to link to each of their books.

    Examples:
    An author post, featuring an author with 28 books attributed: http://wpb.matmartin.co.uk/authors/abie-longstaff/
    A book post by this author: http://wpb.matmartin.co.uk/books/the-fairytale-hairdresser-and-aladdin/

    As you can see, the author post returns all 28 books in the lower portion of the page. The book post should do the same, and I am trying to use the author PO field to access the repeater containing them.

    Here’s my code, from the book post template. So far as I can see I’m using sub_field where I should but maybe I’m wrong about that?

    <?php if(get_field('author')) { ?>
    	<?php
    	$post_object = get_field('author');
    	if( $post_object ): 
    		$post = $post_object;
    		setup_postdata( $post ); 
    		?>
    		<div class="sep" id="lower"></div>
    		<div class="posttitle">
    			<h1>All Titles<span style="color:#777;"> &nbsp;·&nbsp; <a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></span></h1>
    		</div>
    		<?php if( have_rows('books') ): ?>
    		<div class="authbookfeed">
    			<?php while ( have_rows('books') ) : the_row(); ?>
    				<?php
    				$post_object = get_sub_field('title');
    				if( $post_object ): 
    					$post = $post_object;
    					setup_postdata( $post ); 
    					?>
    					<div class="abf-book">
    					    <?php if ( get_the_post_thumbnail($post_id) != '' ) { ?>
    							<div class="abf-cover">
    								<a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('full'); ?></a>
    							</div>
    						<?php } else { ?>
    							<a href="<?php the_permalink(); ?>" class="noimagelink">
    								<div class="noimage"></div>
    							</a>
    						<?php } ?>
    						<div class="abf-cap">
    							<h5 class="abf-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
    							<?php if(get_field('publisher_display')) { ?>
    								<p class="abf-meta"><?php the_field('publisher_display'); ?></p>
    							<?php } else { ;?><?php } ;?>
    							<?php if(get_field('category')) { ?>
    								<p class="abf-meta"><?php the_field('category'); ?></p>
    							<?php } else { ;?><?php } ;?>
    						</div>
    					</div>
    				    <?php wp_reset_postdata(); ?>
    				<?php endif; ?>			
        		<?php endwhile; ?>  
        	</div>
    		<?php else : endif; ?>
    	<?php endif; ?>	
    <?php } else { ;?><?php } ;?>
  • You have a couple problems that are likely causing unpredictable results

    For example, your using the variable name $post_object in both the parent and sub loops. wp_reset_postdata(); needs to be called at the end of your first if ($post_object) {. You’ve also got a couple of nested setup_postdata( $post ); calls that can cause unpredictable results that I explain here https://support.advancedcustomfields.com/forums/topic/nested-post-object-fields/#post-48227

  • thanks john, and apologies for not replying sooner. the client asking for this function changed their mind about needing it so this has become a self-improvement issue for me and thus has slipped in priority.

    thanks for the pointers, i think i see what you mean and although i’ve not had a chance to test anything yet i’ll try and get it working as you suggest.

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

You must be logged in to reply to this topic.