I have this loop where I loop through every post of my CPT and fetch the title and the content of each post and put it into an array. That is no problem.
But as it happens I have created a repeatable field with Advanced Custom Fields that is present in each of these posts. These fields contains images and captions that I want to add to the array.
Here's my current code
$args = array(
'posts_per_page' => '-1',
'post_type' => 'work',
'orderby' => 'ID',
'order' => 'DESC',
);
$data = array('work' => array());
$loop = new WP_Query($args);
if( $loop->have_posts() ):
while( $loop->have_posts() ): $loop->the_post();
$id = $loop->post->ID;
$data['work'][$id] = array(
'title' => apply_filters( 'the_title', $loop->post->post_title ),
'content' => apply_filters( 'the_content', $loop->post->post_content ),
);
endwhile;
endif;
wp_reset_postdata();
Which outputs
{
"work":{
"45":{
"title":"Project 7",
"content":"<p>I am the other text.<\/p>\n"
},
}
}
And what I'm looking to have is something like this
{
"work":{
"45":{
"title":"Project 7",
"content":"<p>I am the other text.<\/p>\n"
"items":{
"1":{
"caption":"I'm an image",
"url":"www"
},
"2":{
"caption":"I'm another image",
"url":"www"
},
}
},
}
The loop that fetches the different attachments looks like this..
<?php if(get_field('work')): ?>
<?php while(has_sub_field('work')):
$attachment_id = get_sub_field('image');
$caption = the_sub_field('caption');
$image = wp_get_attachment_image_src( $attachment_id, work );
endwhile; ?>
<?php endif; ?>
..but I can't wrap my head around how I get the result from this loop into the array. If someone could point me in the right direction I'd be very happy! Thanks.