SOLVED: ACF Repeater inside a custom WP_Query loop doesn't work

Bricks Version: 1.1.3
Browser: Chrome 90
OS: macOS / Windows / Linux / etc.
URL: (a link to a page that illustrates the issue would be really helpful)

I have created multiple custom WP_Query loops that I use on a taxonomy page.
The hierarchy is :

  • A taxonomy (Top List) on a Custom Post (Site)
  • Inside the Taxonomy, an ACF Repeater, with a unique field: a Post Object (CPT Site)
  • On that Page, I used this tutorial (Adding any Custom WP_Query loop to Bricks' Query Loop - BricksLabs) to create a Custom WP_Query Loop (Ref1) to get the post list, in the order of the repeater field.

All works well.

I then have custom fields inside each posts, (description, rating etc etc) and 2 repeaters, one for the pros, one for the cons.

Now In that Custom WP_Query Loop (Ref1) - I can easily call dynamic data (acf_description, acf_dating etc.) but when I loop on the repeaters (and I pick “ACF Repeater: Pros” from the dropdown) and include dynamic data as output (acf_pros_pro) - There is no output.

It seems odds that I can output the ACF fields but not the repeater. This repeater works well on single pages.

Not sure how to start debugging this to see where it comes from?

To reproduce the bug:

  • Create CPT (CPT1) with Custom Fields
  • Create Taxonomy attached to this CPT (TaxCPT1)
  • Add a Repeater as Custom Field with SubField (Rep_Sub_TaxCPT1): Post Object on (TaxCPT1)
  • Create a Custom WP_Query Loop and loop through Rep_Sub_TaxCPT1
  • Try and Loops through a custom field of that queried objact

I have recreated the problem in an empty wordpress, if anyone wants to test, feel free to message me.

Hi

I have the same setup with a cpt and repeaters (inluding sub-repeaters) and it works without any issues.

Just to check, have you selected the correct “ACF Repeater: xxx” query type in the repeating block or div?

Repeater

Bricks shows you all available repeaters.

But I do not use a custom query type in the first query (above the repeater query), so that could be the problem, too.

Cheers

Patric

Hi Patrick,

The problem only occurs when using Custom Query loop

Can you publish here the exact code of your custom query so that I can run it on my site as a test?

/* Add new query type controls to query options */
add_filter( 'bricks/setup/control_options', 'bl_setup_query_controls');
function bl_setup_query_controls( $control_options ) {

    /* Adding new options in the dropdown */
    $control_options['queryTypes']['my_first_custom_query'] = esc_html__( 'My first WP Query' );
	$control_options['queryTypes']['my_second_custom_query'] = esc_html__( 'My second WP Query' );

    return $control_options;

};

/* Run new query if option selected */
add_filter( 'bricks/query/run', 'bl_maybe_run_new_queries', 10, 2);
function bl_maybe_run_new_queries( $results, $query_obj ) {

    if ( $query_obj->object_type === 'my_first_custom_query' ) {
        $results = run_first_query();
    }

	if ( $query_obj->object_type === 'my_second_custom_query' ) {
        $results = run_second_query();
    }

    return $results;

};


/* Setup post data for posts */
add_filter( 'bricks/query/loop_object', 'bl_setup_post_data', 10, 3);
function bl_setup_post_data( $loop_object, $loop_key, $query_obj ) {

    /* setup post data if using any of our custom queries */
    if ( $query_obj->object_type === 'my_first_custom_query' || $query_obj->object_type === 'my_second_custom_query' ) {

       global $post;
       $post = get_post( $loop_object );
       setup_postdata( $post );

    }

    return $loop_object;

};


/* first WP Query arguments */
function run_first_query() {

    /* Add all of your WP_Query arguments here */
		$term = get_queried_object();
    $sites_array = array();
    foreach(get_field( 'tax_repeater', $term ) as $site){
        array_push($sites_array,  $site["taxrep_subfield"]);
    }

    $args = [
        'post_type' => 'custom_post',
        'orderby'        => 'post__in',
        'posts_per_page' => '10',
        'post__in' => $sites_array,
    ];

    $posts_query = new WP_Query( $args );
    return $posts_query->posts;
};

/* second WP Query arguments */
function run_second_query() {

    /* Add all of your WP_Query arguments here */
    $args = [
        'post_type' => 'post',
        'orderby'        => 'rand',
        'posts_per_page' => '1',
    ];

    $posts_query = new WP_Query( $args );

    return $posts_query->posts;

};```

I get a warning message “Warning: foreach() argument must be of type array|object, null given” here:

What are you trying to do with this code inside the query?

It gets all the Post Object in the Tax Repeater Subfield of the Tax repeater.
The Post Object must be returning Post ID

OK, the custom query is working here for me:

…but that php warning from these lines in the query remains…

Warning: foreach() argument must be of type array|object, bool given in /home/httpd/vhosts/…

Yes custom query works.
What doesn’t work is inside that custom query, you add a loop on a repeater that is inside the post you just show.

Screenshot 2023-01-29 at 13.40.36

Try the following…

add a code element within your custom query loop and copy / paste this into the code element:

<?php 
   /* first we check, if the acf repeater has any content */
   if( have_rows('zutaten_repeater') ):
             
             /* if it has content, we loop over all content */
             while ( have_rows('zutaten_repeater') ) : the_row();
           
     
             if( have_rows('zutatenliste') ):
			
			 /* if it has content, we loop over the first line */
             for ($i = 1; $i <= 1; $i++) {
             the_row();
             
             /* as an example, we get a text field from the sub-repeater and print it */
             $sub_repeater = get_sub_field('zutat'); 

             echo '<div>'.$sub_repeater.'</div>';

			 }
     
           endif;
         
           endwhile;
        
    else :
    
    /*inform the user, if the repeater has no content */
    echo "Sorry, no rows found in the repeater";

    endif;
?>

Replace the zutaten_repeater, the zutatenliste and zutat (these are all my acf repeater fields) with your repeater field.

Then check if content from your repeaters are shown or not.

Cheers

Patric

Thank you for the code.
I put the following code based on your example in within my Custom Query Loop.

<?php 
   /* first we check, if the acf repeater has any content */
   if( have_rows('cf_repeater') ):
             
             /* if it has content, we loop over all content */
             while ( have_rows('cf_repeater') ) : 

									the_row();
									echo get_sub_field('subfield');
     
         
           	endwhile;
        
    else :
    
    /*inform the user, if the repeater has no content */
    echo "Sorry, no rows found in the repeater";

    endif;
?>

Actualy outputs the subfield.

So it seems that the Query Loop “ACF Repeater: CF Repeater” is fault since it doesn’t output anything itself

It’s a bit bothering because I had elements within that loops. The only way I see it now is going full code to output those elements, which isn’t going to be easy with the responsive design

I still think that the problem comes from the custom query. If you use the standard query for repeaters it works.

So I would concentrate on the custom query.

Maybe this helps:

See the comments and code in the lower part of that page.

Cheers

Patric

I’m not sure I understand. The custom query actually return the good posts and output them. It can actually return ACF fields, unless it’s a repeater. So what exactly would be the cause?

Also, I noted that this is output only in the builder, not in the front end.

Moifying the code to make it as simple as possible

function run_first_query() {


    $args = [
        'post_type' => 'custom_post',
        'posts_per_page' => '10',
    ];


    $posts_query = new WP_Query( $args );
    return $posts_query->posts;
};

results in the same initial problem (though there is no warning for the loop)

What are you trying to achieve with the custom query that the standard query cannot do?

I mean you just want to loop over the posts from the custom post types, right?

Yes, I need them in a particular order - The order from the repeater field.

Maybe you can use the filter acf/load_value from here:

and make a hook in Bricks like this example (at the bottom) with add_filter:

Patric

I do not need to sort the repeater field, as the order is the good one yet.
I need to be able to call it within in a Custom Loop Query so I can get that list. So Far, it doesn’t work, only in pure php + html. That’s why I’m wondering why Bricks cannot do that.