0

Have a custom post type, where each post is a gallery, and each has an Advanced Custom Fields Gallery field that has say 5-20 images each. Also have a custom post type post template as seen below (stripped out most of the code to focus on parts that are relevant to this.) The comments in the code explain which each part does. Overall it appears to all work, however no matter what post I'm on in this custom post type, such as example.com/gallery/gallery-1, example.com/gallery/gallery-2, example.com/gallery/gallery-3, etc. It shows the same featured image and same thumbnails on each page. It appears to be pulling the first gallery from the first post, rather than on each page checking to pull in the gallery items associated with that page.

I'm sure it's simple such as a matter of needing to flush/reset a loop or such, but struggling to find the right way to describe it to find the answer on Google. Had it working originally, but once I had to add loops inside of loops the issue started. If anyone has any idea would great. Will keep researching in the meantime.

<?php get_header(); ?>

        <div id="main">
            <?php
                    //Checks gallery custom post type and finds the current pages entry
                    $args = array( 'post_type' => 'customers_gallery', 'posts_per_page' => 1 );
                    $loop = new WP_Query( $args );
                    while ( $loop->have_posts() ) : $loop->the_post(); ?>

                <div>
                    <?php
                     //Looks up the advanced custom field gallery associated with this post
                        $hero = get_field('gallery_images'); ?>
                        <!-- //Show the first image in that gallery -->
                        <img src="<?php echo $hero[0]['url']; ?>" class="main-image" style="width: 484px;">
                        <!-- //Shows title of this page -->
                        <h3><?php the_title(); ?></h3>

                        <?php
                        //Looks up the advanced custom field gallery associated with this post
                        $images = get_field('gallery_images');

                        //Loops through all the gallery image showing a thumbnail for each and each is linked to its full size
                        if( $images ): ?>
                            <ul>
                                <?php foreach( $images as $image ): ?>
                                <li>
                                    <a href="<?php echo $image['url']; ?>">
                                <img src="<?php echo $image['sizes']['gallery-landscape-thb']; ?>" alt="<?php echo $image['alt']; ?>" />
                              </a>
                                </li>
                                <?php endforeach; ?>
                            </ul>
                            <?php endif; ?>


                            <div>
                                <ul>
                                    <?php
                                    //Looks up all the pages in this custom post type and lists the titles of the first 12 and linked to respective pages
                                    $custom_query = new WP_Query(
                                    array(
                                        'post_type' => 'customers_gallery',
                                        'posts_per_page' => 12
                                    )
                                    );
                                    if ( $custom_query->have_posts() ) : while ( $custom_query->have_posts() ) : $custom_query->the_post(); ?>
                                            <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                            <?php endwhile; endif; wp_reset_query(); ?>
                                </ul>
                            </div>
                </div>

                <?php endwhile; ?>

        </div>
        <!-- /#main -->



<?php get_footer(); ?>

UPDATE

Some have noted to add a reset, but I'm still thinking the issue is something else. Since I can remove nearly all the code in template and get it down just to the following and it still echos the same title no mater which post im viewing in the gallery custom post type (it shows the first title in the list of posts in the gallery custom post type, where is it should pull in the title for that particular page. The title in the tab is correct.

<?php get_header(); ?>


      <?php
            $args = array( 'post_type' => 'customers_gallery', 'posts_per_page' => 1 );
            $loop = new WP_Query( $args );
            while ( $loop->have_posts() ) : $loop->the_post(); ?>

            <?php echo the_title(); ?>

        <?php endwhile; ?>

<?php get_footer(); ?>

UPDATE 2

Returned to the original code that I posted in this message and simply removed the code noted in my second post aka, that $args = array( 'post_type' => 'customers_gallery'... and all works. So seems the issue was I was trying to create a loop for the custom post type but looks like that is not needed when using a single-[custom-post-type-name].php.

1

So, in researching your issue, I stumbled across another answer on Wordpress StackExchange here: Resetting post data to previous loop in nested loops

But to solve your problem specifically, you need to reset your first WP_Query's specific post data after your 2nd WP_Query is complete. Also, you should only need to reset the postdata if you are within your if statement, not after it:

Replace:

<?php endwhile; endif; wp_reset_query(); ?>

with:

<?php endwhile; $loop->reset_postdata(); endif; ?>

Side note: Since your second WP_Query doesn't actually have anything to do with the first WP_Query, I would either move your second WP_Query ($custom_query) outside of the first one's while loop.

  • Thanks so much for getting back so quick. I swapped that out for the other, and still shows the same. But I'm going to checkout that link you shared now to see if there is anything else afoot in my code. – cchiera Sep 7 '17 at 21:17
  • I tried removing the nested loop and same issue so think it may relate to "$args = array( 'post_type'"... section where its simple polling the first of that custom post type rather than the one for the page the user is currently on. Thinking I'm not using the main loop properly or something. Going to keep researching too. – cchiera Sep 7 '17 at 21:23
  • Looks like it wasn't anything to do with the reset, but rather it was I didnt need to do that initial WP_Query at all since it must automatically be done by wordpress. After removing it everything is working properly. I'm updated my post with the answer. But going to mark this as correct, as the reset_postdata maybe helpful for people with similar but slightly different code. Thank you! – cchiera Sep 8 '17 at 0:24
1

There's likely a bit more going on than just this, but socki03 is on track.

You need to replace

<?php endwhile; endif; wp_reset_query(); ?>

with

<?php endwhile; wp_reset_postdata(); endif; ?>

The big problem is that you need to go through all your custom code and make sure you have reset the postdata on Each custom wp_query you've made. All of them need to be reset or you'll be pulling the wrong query throughout the site.

Check every wp query and make sure that after the endwhile you're using wp_reset_postdata();

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.