0

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.

2

I don't think this is a restrict Wordpress question, but you might try

$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;

        $attachments=array();
        if(get_field('work')): 
           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' );   
              $attachments[$attachment_id]=array("caption"=>$caption,"url"=>$image); 
           endwhile;    
        endif; 

        $data['work'][$id] = array(
            'title' => apply_filters( 'the_title', $loop->post->post_title ),
            'content' => apply_filters( 'the_content', $loop->post->post_content ),           
            'items'  => $attachments;  
        );
    endwhile;
endif;
wp_reset_postdata();
  • Thank you so much! You are right that isn't a specific Wordpress question. But I still decided to put it here rather than on Stack Overflow since there are alot of WP specific stuff in this particular case. Thanks again! – INT Mar 31 '13 at 14:50
  • ok great ;-) ps: I assume the work part in wp_get_attachment_image_src( $attachment_id, work ) should be 'work' ? – birgire Mar 31 '13 at 14:53
0

Put the second loop in a function:

$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 ),
            'items' => get_acf_field( $id ),
        );

    endwhile;
endif;

function get_acf_field( $post_id ) {

  $result = array();

  if( get_field( 'work', $post_id ) ) {

    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 );

      $result[] = array( 'caption' => $caption, 'url' => $image );

    }
  }

  return $result;

}

I hope this helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.