• Hi,

    I need to fix a nested loop: first of all i’m getting all posts and in the loop of posts i’m getting al attachments (images) twice but in a different order.

    The problem is it only works on the first item and not on the second. How do i fixed this properly?

    <div class="content">
        <?php
        $args = array('posts_per_page' => 5, 'offset' => 0);
        $projectCount = 0;
        $myposts = get_posts($args);
        foreach ($myposts as $post) : setup_postdata($post);
        $projectCount++;
            ?>
            <section class="project" id="<?php echo $projectCount ?>">
                <h2><?php the_title(); ?></h2><p><strong><?php echo wp_strip_all_tags(preg_replace('/<img[^>]+./', '', $post->post_content)); ?></strong></p>
                <div class="colHolder">
                <?php
                $media = get_posts(array(
                    'post_parent' => $post->ID,
                    'post_type' => 'attachment',
                    'post_mime_type' => 'image',
                    'orderby' => 'title',
                    'order' => 'ASC'
                ));
                //print_r($media);
                ?>
                <?php foreach($media as $key => $image): ?>
                    <?php if(!empty($image->post_title)): ?>
                    <div class="col<?php if(!empty($image->post_content)){echo $image->post_content;}else{echo 1;} ?> colImg">
                        <div class="img imgGrid" data-order="<?php echo $image->post_excerpt; ?>" style="background: url(<?php echo $image->guid ?>) 50% 50% no-repeat;">
                            <img src="<?php echo $image->guid ?>" alt="" />
                        </div>
                    </div>
                    <?php endif; ?>
                <?php endforeach; ?>
    
                <ul class="hiddenGal">
                <?php
                    $allmedia = get_posts(array(
                        'post_parent' => $post->ID,
                        'post_type' => 'attachment',
                        'post_mime_type' => 'image',
                        'orderby' => 'caption',
                        'order' => 'ASC'
                    ));
                    //
                    foreach($allmedia as $key => $imagel):
                ?>
                    <li data-img="<?php echo $imagel->guid ?>" class="hiddenImage"></li>
                <?php endforeach; ?>
                </ul>
                </div>
            </section>
    
    <?php
    endforeach;
    wp_reset_postdata();
    ?>
    
    </div>
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You mean the second get_posts() for attachments is not working, right?

    Probably because ‘caption’ is not a valid ‘orderby’ argument. Unlike in SQL where you can use any column name, there is a limited set of orderby arguments for get_posts().
    https://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters

    get_posts() uses WP_Query, so any valid WP_Query argument will work in get_posts().

    Attachments store captions in ‘post_excerpt’, but that’s not a valid orderby argument either. What you can do is alter the SQL query generated by get_posts() through the ‘posts_request’ filter. Add your filter callback just before calling the second attachment get_posts(), then remove it immediately afterwards so the callback does not alter any other post queries.

Viewing 1 replies (of 1 total)
  • The topic ‘Nested loop get_posts(’ is closed to new replies.