- PHP 7.2
- Wordpress 4.9.8
- Advanced Custom Fields 5.7.7
I'm interested in creating an array where each item would hold:
- post id
- post title
- array of images belonging to post
I am using an ACF repeater for every post that holds many images, the repeater name is carousel.
There is no connection between the WP post object and the ACF fields.
The issue:
nested foreach pushes all the images into the first post.
Expected:
nested foreach will fill the $randomArray only with images that belong to that post ID.
$workshop_posts_args = array(
'post_type' => 'workshops'
);
$randomArray = [
'post_id' => '',
'post_title' => '',
'post_image_url' => []
];
$post_query = new WP_Query($workshop_posts_args);
if ($post_query->have_posts()) {
while ($post_query->have_posts()) {
$post_query->the_post();
$carousel_array = get_field('carousel', get_the_ID());
echo "<h2>".get_the_title()."</h2>";
if ($carousel_array) {
foreach ($carousel_array as $carousel_images) {
foreach ($carousel_images as $image) {
$randomArray['post_id'] = get_the_ID();
$randomArray['post_title'] = get_the_title();
$randomArray['post_image_url'][] = $image['url'];
echo 'image_url:'.$image['url'].'<br>The array: <pre>'.print_r($randomArray, true).'</pre>';
?>
<?php
}
}
}
}
}
?>
<h1>TOTAL ARRAY</h1>
<pre><?php print_r($randomArray) ?></pre>