Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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();
share|improve this answer
    
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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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