Support

Account

Home Forums ACF PRO Post Object inside Repeater on a Taxonomy template returns first instance only

Solving

Post Object inside Repeater on a Taxonomy template returns first instance only

  • Ok, so ive seen 2 others with this similar issue, only 1 got marked as resolved, however that was just on a standard template, not taxonomy template.

    Im also using this same code (almost), elsewhere on my website, and its working as expected. But on the Taxonomy template, its only returning the first repeater row.

    Any idea’s as to why?

    <?php
    if( have_rows('services_managers', $term_id) ):
    	while ( have_rows('services_managers', $term_id) ) : the_row(); ?>
    
    	<?php
    
    	$post_object = get_sub_field('service_manager_name');
    
    	if( $post_object ): 
    
    // override $post
    		$post = $post_object;
    		setup_postdata( $post ); 
    
    		?>
    
    		<div class="m-all t-1of2 d-1of2 first-col">
    			<?php 
    			$staffthumb_id = get_post_thumbnail_id(get_the_id());
    			$staffthumb_url_array = wp_get_attachment_image_src($staffthumb_id, 'square-thumb', true);
    			$staffthumb_url = $staffthumb_url_array[0];
    			?>
    			<img src="<?php echo $staffthumb_url; ?>" alt="<?php echo $image['alt']; ?>" class="full-width matchheight_01" />
    		</div>
    
    		<div class="m-all t-1of2 d-1of2 last-col">
    			<div class="project_manager_info matchheight_01">
    				<div class="vertical">
    					<h2 class="team_name bold"><?php the_title(); ?></h2>
    					<span class="team_job_title block"><?php the_field('job_title', get_the_id()); ?></span>
    					<span class="team_email_address block"><?php the_field('team_email', get_the_id()); ?></span>
    				</div>
    			</div>
    		</div>
    
    	<?php endif; ?>
    
    <?php   endwhile;
    endif;
    ?>
    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>

    The working code elsewhere on my site (non-taxonomy) just has the “, $term_id” removed from the have_rows section and works fine.

    Any help is appreciated!

  • Hi @miked89

    Just a hunch, but it could be that when you set up the new post data, it overrides the $id of the $post_object = get_sub_field('service_manager_name');. I would store your $post_id as a variable before your while loop and then use that to set the get_sub_field('service_manager_name', $post_id)

    Phil

  • Hi Phil,
    Thanks for the help, a little unsure how to implement the solution though.

    Would it be saving the Tag ID into the variable as the ACF Repeater is on a custom taxonomy page?

  • What Phil means is (If I understand it correctly 😉 ) is define something like $post_id right after the while.

    $post_id = get_current_ID();

    Then use get_sub_field('service_manager_name', $post_id) instead of get_sub_field('service_manager_name').

    -=-

    Maybe I’m way off, but why is it necessary to setup the post_data ?

    $post is not used in the given code and I don’t see any functions which can’t run without this being setup.

  • @miked89 & @beee

    i thought it needed a $post_id, but you’re fetching fields from a term not a post. You can find your solution here: https://www.advancedcustomfields.com/resources/adding-fields-taxonomy-term/

    Basically you’re not passing the proper value to the second parameter.

  • I missed that part as well to be honest… guess I should read more carefully 😉

  • Yeah im trying to pull this from a taxonomy term, and it is returning results. But its just pulling the first row of the repeater.

    So is the error in the Repeater section or the Post Object section?

    If i remove the setup_postdata($post); line, it returns the correct number of rows, just without any data inside of them.

  • i think the have_rows('services_managers', $term_id) doesn’t seem correct. try passing the whole $term object and see if that fixes it. if not, then just don’t setup the post data.

    if you alread have the $post object, then you ahve the title $post->post_title and the id $post->ID.

    https://codex.wordpress.org/Class_Reference/WP_Post

  • I agree with Nathan…

    I would use it differently. I would do something like this I think.

    $term = get_queried_object();
    $var = get_field('field_name', $term);

    Then loop through $var.

    if ( count( $var ) > 0 ) ) {
        $manager_name = get_sub_field('service_manager_name');
        if ( $manager_name ) :
          // do stuff
        endif;
    }

    Note this is from the top of my head, so untested.

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

The topic ‘Post Object inside Repeater on a Taxonomy template returns first instance only’ is closed to new replies.