Search code examples
phpwordpresswordpress-rest-api

foreach loop returns only one result of post


I'm trying to show 4 posts from each category. Tried this code which should fetch 4 posts from respective categories. then sort them as required in col1, col2, and so on.

<?php

    $col1 = $col2 = $col3 = $col4 = $col5 = array();
    $parsha_terms = get_terms('parsha');

    foreach($parsha_terms as $term) {
        $order = get_field('parsha_order', 'term_' . $term->term_id);
        $column = get_field('parsha_column', 'term_' . $term->term_id);
        $color_pdf = get_field('pdf', 'term_' . $term->term_id);
    
        $posts = get_posts(
            array(
                'cat' => $cat_id,
                'tax_query' => array(
                    array(
                        'posts_per_page' => 4,
                        'taxonomy' => 'parsha',
                        'field' => 'term_id',
                        'terms' => $term->term_id,
                    )
                )
            )
        );
    
        $data = array(
            'term_id'    => $term->term_id,
            'name'       => $term->name,
            'order'      => $order,
            'column'     => $column,
            'post_ids'   => $posts[0]->ID,
            'post_title' => $posts[0]->post_title,
            'color_pdfs'  => $pdfcolor,
            'color_pdf_link'  => $color_pdf,
        );

        if($column == 1) {
            array_push($col1, $data);           
        }
        else if($column == 2) {
            array_push($col2, $data);
        }
        else if($column == 3) {
            array_push($col3, $data);
        }
        else if($column == 4) {
            array_push($col4, $data);
        }
        else if($column == 5) {
            array_push($col5, $data);
        }   
    }

then in output, it shows only one result(post) of each category, but it should show 4.

<?php foreach($col1 as $item) { ?>
    <li>
    <p><?= $item['name']; ?></p>
    <a href="<?php  echo $link;  ?>"><?= $item['post_title']; ?></a>    
    </li>
<?php } ?>

Solution

  • You're retrieving an array of $posts but you're then only adding the first element $posts[0] to your $data array.

    You need to loop them with foreach

    $posts = get_posts(
        array(
            'cat' => $cat_id,
            'tax_query' => array(
                array(
                    'posts_per_page' => 4,
                    'taxonomy' => 'parsha',
                    'field' => 'term_id',
                    'terms' => $term->term_id,
                )
            )
        )
    );
    
    foreach($posts as $post) {
        $data = array(
            'term_id'    => $term->term_id,
            'name'       => $term->name,
            'order'      => $order,
            'column'     => $column,
            'post_ids'   => $post->ID,
            'post_title' => $post->post_title,
            'color_pdfs'  => $pdfcolor,
            'color_pdf_link'  => $color_pdf,
        );
    
        if($column == 1) {
            array_push($col1, $data);           
        }
        else if($column == 2) {
            array_push($col2, $data);
        }
        else if($column == 3) {
            array_push($col3, $data);
        }
        else if($column == 4) {
            array_push($col4, $data);
        }
        else if($column == 5) {
            array_push($col5, $data);
        }   
    }