1
  • 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>
2
  • @AlivetoDie this is the result: pastebin.com/SFZ7pZu6 Commented Nov 2, 2018 at 11:56
  • i have got your problem and updated my code in answer.check now Commented Nov 2, 2018 at 11:58

2 Answers 2

1

You are over-writing array index again and again inside loop and that's you problem.

so do:-

$randomArray = []; //before post_query

And change if block like below:-

if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        $id = get_the_ID();
        $randomArray[$id]['post_id'] = $id;
        $randomArray[$id]['post_title'] = get_the_title();
        $carousel_array = get_field('carousel', $id);
        if ($carousel_array) {
            foreach ($carousel_array as $carousel_images) {
                foreach ($carousel_images as $image) {
                    $randomArray[$id]['post_image_url'][] = $image['url'];
                    ?>
                    <?php
                }
            }
        }
    }
}

Note:- rest code will be same

the above code will give you post-id based multi-dimensional array. if you want indexes to be 0,1,2,3..... format then do:-

$randomArray = array_values($randomArray);
Sign up to request clarification or add additional context in comments.

2 Comments

perfect. Now I know better how to treat these arrays, thanks !
@clusterBuddy glad to help you :):)
1

Use proper indexing of $randomArray like below:

<?php
  $workshop_posts_args = array(
  'post_type' => 'workshops'
);

$randomArray = array();
$post_query = new WP_Query($workshop_posts_args);
$index = 0;
if ($post_query->have_posts()) {
    while ($post_query->have_posts()) {
        $post_query->the_post();
        $randomArray[$index]['post_id'] = get_the_ID();
        $randomArray[$index]['post_title'] = get_the_title();
        $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[$index]['post_image_url'][] = $image['url'];
                //echo 'image_url:'.$image['url'].'<br>The array: <pre>'.print_r($randomArray, true).'</pre>';
                ?>
                <?php
            }
        }
    }
    $index++;
}
}
?>
<h1>TOTAL ARRAY</h1>
<pre><?php print_r($randomArray) ?></pre>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.