Home Forums Add-ons Repeater Field Object field inside a repeater not working right.


Object field inside a repeater not working right.

  • Hi,

    I have a problem with objects field inside a repeater field.
    I want to display some fields from this object, they’re from articles from different custom types.

    I don’t know what’s going on, the first item is OK, but after that, it’s not listing the right information, I can’t get the right rows.

    Here’s my code :
    // get the main custom types
    <?php $args = array (‘post_type’ => ‘newslettre’, ‘order’ => ‘DESC’,’posts_per_page’ => ‘1’, );
    $my_query= new WP_Query( $args );
    while ( $my_query->have_posts() ) :
    $my_query->the_post(); ?>

    // get the repeater fields

    <?php if( have_rows(‘articles’) ): ?>
    <?php while ( have_rows(‘articles’) ) : the_row(); ?>
    <?php $post_object = get_sub_field(‘choisir_un_article’); ?>
    <?php if( $post_object ): ?>
    <?php foreach ($post_object as $post) {
    $single_attachment_id = get_field(‘image’);
    $singleurl = wp_get_attachment_image_src( $single_attachment_id, ‘large’ );
    $single_photo = $singleurl[0]; ?>

    // display the title and images
    <?php the_title(); ?>

    <?php }
    wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php endwhile; ?>
    <?php wp_reset_postdata(); ?>
    <?php endif; ?>
    <?php endwhile; ?>

    Any help would be very appreciated !
    Thank you

  • Hi fgenois,

    It seams you have the same issue as explain here. As I told to Dada, you probabely kill the query loop by using setup_postdata(), plus, when you want to get_field in a loop which is in a loop (and again and again) it’s better to call the field with the post ID.

    Try this (same logic as the Dada issue, not functional, only to show the logical path) :

    $args = array(...);
    $query = new WP_Query( $args );
    if ( $query->have_posts() ) {
    	while ( $query->have_posts() ) {
    		$query->the_post() ;
    		$query_post_id= get_the_ID();
    		if( have_rows('repeater_you_wan', $query_post_id)) { 
    					while ( have_rows('repeater_you_wan', $query_post_id)) : the_row(); // get row with the query_id to avoid mistake
    						[...] // Write your html
    						$posts_from_repeater = get_sub_field('your_multiple_post_object_subfield');
    						if($posts_from_repeater) {
    							foreach ($posts_from_repeater as $sub_post) { // Where $sub_post is post object
    								$sub_post_id = $sub_post->ID;
    								$my_post_field_data = get_field('my_post_field_data', $sub_post_id);// get field with the query_id to avoid mistake
    								$my_post_title = get_the_title($sub_post); // can get title by post object
    								[...] // Write your html
    								[...] // Write your html
    								[...] // Write your html
    						[...] // Write your html

    PS : when you write code, use the code balise it’s will be easier to read.

  • Hello,

    I knew the problem was coming from setup_postdata thing, I just didn’t know how to solve it. I used your code and everything’s now working !!

    Thank you very much !

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

The topic ‘Object field inside a repeater not working right.’ is closed to new replies.