Home > Blockchain >  Parse post object inside repeater field in ACF/Wordress
Parse post object inside repeater field in ACF/Wordress

Time:12-14

Im trying to get the title etc from a post object which inside a repeater field.

    $classes = get_field('classes'); //repeater field containing a sub field named "class" (post object).

    <?php foreach($classes as $class) : ?>
      <?php 
        echo $class; //returns post objects as arrays.
        echo $class['title']; //returns nothing.
        echo $class['post_title']; //returns nothing.
      ?>
    <?php endforeach; ?>

What gets returned:

[class] => WP_Post Object
    (
        [ID] => 57
        [post_author] => 1
        [post_date] => 2021-12-07 23:55:28
        [post_date_gmt] => 2021-12-07 23:55:28
        [post_content] => fffdfdf
        [post_title] => testa
        [post_excerpt] => dfdsgdsgf
        etc.....
    )

So how do i get the post titles etc?

CodePudding user response:

According to ACF documentation this is how you loop through a Repeater field and load a sub field value: ACF | Repeater

// Check rows exists.
if( have_rows('classes') ):

    // Loop through rows.
    while( have_rows('classes') ) : the_row();

        // Load sub field value.
        $product = get_sub_field('class');

        // Access product info.
        $product_id   = $product->get_id();
        $product_name = $product->get_name();

    // End loop.
    endwhile;

// No value.
else :
    // Do something...
endif;

List of methods to access product information.

  • Related