Support

Account

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

Solved

Object field inside a repeater not working right.

    • fgenois

    • December 26, 2018 at 9:32 am

    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) {
    setup_postdata($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(); ?>
    “>
    height=”300″>

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

    Any help would be very appreciated !
    Thank you

    • feyfey

    • December 29, 2018 at 8:46 am

    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
    					endwhile;
    		}
    	}
    }
    wp_reset_postdata();

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

    • fgenois

    • January 8, 2019 at 4:40 am

    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)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.