Support

Account

Home Forums Front-end Issues Can only get 1 post object to show in repeater field that contains 2 post object

Solving

Can only get 1 post object to show in repeater field that contains 2 post object

  • Hi,

    I have a custom post type called ‘weeks’. I’m running a custom wp_query on a unique page template. That code is:

    <?php
        $args = array(
          'post_type' => 'weeks',
          'posts_per_page' => 1
        );
    
        global $post;
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
    
        ?>
    
    <?php endwhile; ?>
      <?php wp_reset_query(); ?>
    

    That is working fine and showing only the latest “Week”. Inside of a Week post there is a repeater field called ‘monday_posts’. I’ve called that inside the while loop like so:

    <?php if( have_rows('monday_posts') ): ?>
    
          <div class="monday">
    
            <?php while( have_rows('monday_posts') ): the_row(); ?>
    
              <h1>test</h1>
    
            <?php endwhile; ?>
    
          </div>
    
        <?php endif; ?>

    In the backend I’ve got my repeater field having a post_object field. I’ve added 2 posts and that works and displays 2 h1’s with test.

    When I try and add a post object to my code it only shows the first one. Not the second or any others that I try to add. That code is:

    <?php
    
                $post_object = get_sub_field('content');
    
                if( $post_object ):
    
                	// override $post
                	$post = $post_object;
                	setup_postdata( $post );
    
                	?>
                    <div>
                    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    </div>
                    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                <?php endif; ?>

    My entire code is below. Any help would be greatly appreciated. Thanks

    ENTIRE CODE:

    <?php
        $args = array(
          'post_type' => 'weeks',
          'posts_per_page' => 1
        );
    
        global $post;
    
        $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();
    
        ?>
    
        <?php if( have_rows('monday_posts') ): ?>
    
          <div>
    
            <?php while( have_rows('monday_posts') ): the_row(); ?>
    
              <?php
    
                $post_object = get_sub_field('content');
    
                if( $post_object ):
    
                	// override $post
                	$post = $post_object;
                	setup_postdata( $post );
    
                	?>
                    <div>
                    	<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    </div>
                    <?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
                <?php endif; ?>
    
            <?php endwhile; ?>
    
          </div>
    
        <?php endif; ?>
    
      <?php endwhile; ?>
      <?php wp_reset_query(); ?>
  • This is a common error/problem when running a custom query on a page. When you use wp_reset_postdata() or wp_reset_query() they do not reset the query to your query, they reset the query to the main WP query. This results in the second itteration of the have_rows loop to look at something that is not the post in your custom query. These are highly misunderstood functions.

    When running a custom query your nested loop for the rows cannot use setup_postdata(), and the reset function and you need to loop through the posts in another way.

    
    $post_object = get_sub_field('content');
    if ($post_object) {
      ?>
        <div>
          <h3><a href="<?php 
              echo get_permalink($post_object->ID); ?>"><?php 
              echo get_the_title($post_object->ID); ?></a></h3>
        </div>
      <?php 
    }
    
  • Ok. So how do I go about making this work?

    If I remove setup_postdata( $post ); then it doesn’t show anything.

  • That’s what I gave you a short example of. You need to substitute calling functions that allow supplying the current post ID rather than depending on setup_post_data() to populate $post.

    The examples on this site are set up assuming that the main WP query is what your using and not a nested query. They are not good examples if you have queries nested to the 3rd level or more running, which is basically what you have going here.

  • Ok great.

    Is having nested queries the right way to go about it or is there another method that would be more efficient?

    Is there any real world examples or what should I be googling?

    Thanks

  • I found this and I’ve seen that you have already commented on it.

    https://support.advancedcustomfields.com/forums/topic/nested-post-object-fields/

    Brotsky_Pixie said that he managed to sort it out but didn’t explain what he did.

    Any ideas? Thanks

  • Having the extra query really depends on what you’re doing.

    You say that weeks is a post type. I would probably look at using the template file “archive-weeks.php” https://developer.wordpress.org/themes/basics/template-hierarchy/ and using a pre_get_post filter to modify the main WP query https://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts. Use the main query to my advantage rather than ignore it.

    But like I said, that depends on exactly why you’re doing it the way your doing it. I don’t know all the details.

  • Basically what I’m trying to accomplish is a Weekly Planner. I’ve got custom post types for posts like Recipes, Workouts, Challenges etc and then a Weeks custom post. A member logs into their account and they have their weekly breakdown. Monday will have a Workout post, a breakfast recipe, a lunch recipe and a dinner recipe.

    In the backend inside the current “week” custom post type. I’ve set up repeater fields for Monday, Tuesday and so on. The admin can then select post objects for each day and add as many as they want through the repeater field.

    I’ve attached a screenshot to show the process. If there is any other better way to achieve then the I’m all ears.

    Thanks so much for your help thus far.

  • Still stuck on this. Does anybody have any further suggestions? Thanks

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

The topic ‘Can only get 1 post object to show in repeater field that contains 2 post object’ is closed to new replies.