Support

Account

Home Forums General Issues Get a repeater of post_objects data from within a post_object

Helping

Get a repeater of post_objects data from within a post_object

  • I’m not sure if this can even be done. I’ll try to explain as best I can;
    I have two ACF custom template setups, “courses” and “lessons”.
    In courses posts I have a repeater with post_object selector to assign all the lessons to a course.
    In lessons posts I have a single post_object selector to assign a parent course to the lesson.
    I want to list all the lessons assigned to a course on the (lessons page) from the repeater info on the (parent course page).

    In other words: on the lessons page > get parent course object > get the custom repeater in the course object > loop through the post objects in the repeater to get the name and link of the assigned lessons.

    I’m not trying to get a post_object from within a repeater. I’m trying to get a repeater of post_objects data from within a post_object.

    Frankly, I’m stumped. Can this be done?

    <?php 
       // START post_object parent course info
       $post_object = get_field('parent_course');
       if( $post_object ): 
       $post = $post_object;
       setup_postdata( $post );
              
       // get the repeater on the parent course page
       $lesson_posts = get_field('lesson_select');
          if( $lesson_posts ):
          $rows = get_field('lesson_select');
             foreach( $rows as $row ) {
             // try to get the title of the post object in the repeater on the parent course page :(
             $post_object = get_sub_field('lesson_individual');
             $post = $post_object;
             setup_postdata( $post );
             $lesson_title = the_title();
               echo '<p>';
               echo $lesson_title;
               echo '</p>';
             }
             wp_reset_postdata();
             endif;
    ?>
    <a class="btn btn-primary btn-block" href="<?php the_permalink(); ?>">Back - <?php the_title(); ?></a>
    <?php 
       wp_reset_postdata(); 
       endif;
       //END post_object parent courseinfo
    ?>
  • The main issue is that you cannot nest setup_postdata() and wp_reset_postdata() the way you have done. The reason is that wp_reset_postdata() does not reset to the previous post in the nesting, it always resets to the post of “The Loop” (the main WP loop showing the post for the current page).

    You need to get values from other posts in some other way than using this method

    You are also using get_sub_field() without having a have_rows() loop. You cannot get the repeater using get_field() and then use get_sub_field() to get a value from the repeater rows. You either need to use the array returned by get_field() $row['lesson_individual'] or you need to use a have_rows() loop.

    The next issue is that your using $post_object for multiple uses and the second one is overwriting the value of the first use.

    
    $post_object = get_field('parent_course');
    // then in the nested loop
    $post_object = get_sub_field('lesson_individual');
    

    And then there is an issue using the_title(). this function always echos the title and it cannot be used in this syntax

    
    $lesson_title = the_title();
    

    removing the issues results in something like:

    
    $post_object = get_field('parent_course');
    if ($post_object) {
      if (have_rows('lesson_select', $post_object->ID)) {
        while (have_rows('lesson_select', $post_object->ID)) {
          the_row();
          $lesson_object = get_sub_field('lesson_individual');
          $lesson_title = get_the_title($lesson_object->ID);
          echo '<p>',$lesson_title,'</p>';
        }
      }
    }
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Get a repeater of post_objects data from within a post_object’ is closed to new replies.