Title: WP_Query within a repeater
Last modified: August 31, 2016

---

# WP_Query within a repeater

 *  Resolved [onetrev](https://wordpress.org/support/users/onetrev/)
 * (@onetrev)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/query-within-a-repeater/)
 * Just trying to figure out how, if possible, I can get a custom WP_Query to work
   within a repeater. When I try to do it, what happens is everything after that
   query is the content from the final item in the original AJAX Load More.
 * [https://wordpress.org/plugins/ajax-load-more/](https://wordpress.org/plugins/ajax-load-more/)

Viewing 6 replies - 1 through 6 (of 6 total)

 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386176)
 * I’ve never tried this but I can see how it might be problematic.
 * What does your query look like inside the repeater?
 *  Thread Starter [onetrev](https://wordpress.org/support/users/onetrev/)
 * (@onetrev)
 * [10 years, 3 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386203)
 * Sure thing. It’s pretty long so here is a link to the Gist I created:
 * [https://gist.github.com/onetrev/c44850751ec0a00678761263999888e8](https://gist.github.com/onetrev/c44850751ec0a00678761263999888e8)
 * Let me know if there is anything else you need. Would be super handy to figure
   out a way to get this to work.
 *  Thread Starter [onetrev](https://wordpress.org/support/users/onetrev/)
 * (@onetrev)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386345)
 * Sorry to bump this, just wondering if you had any further thoughts on whether
   this was possible? It’s in a theme repeater if that makes any difference. Thanks!
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386350)
 * Hi,
    I was able to get a nested query working. Here is my test repeater template.
 *     ```
       <article>
       <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
       <?php the_content(); ?>
       <ul>
       <?php
       $args = array(
       'post_type'                => 'page',
       'posts_per_page'           => 3,
       );
       $test_query = new WP_Query( $args );
       while ($test_query->have_posts()): $test_query->the_post();
       global $post;
       ?>
       <li class="no-img"><a href="<?php the_permalink(); ?>"><?php echo $post->ID; ?> - <?php the_title(); ?></a></li>
       <?php endwhile; wp_reset_postdata(); ?>
       </ul>
       </article>
       ```
   
 * I believe your issue is that you need to pass a $post value to your custom field
   functions in order to return the correct data.
 * Notice the global `$post;` in my template, and then the `wp_reset_postdata()`.
   
   [https://gist.github.com/dcooney/357246e89c9e1b8ed4dc3bf395c07406](https://gist.github.com/dcooney/357246e89c9e1b8ed4dc3bf395c07406)
 * Can I ask why you just don’t create this query using the shortcode builder?
 *  Thread Starter [onetrev](https://wordpress.org/support/users/onetrev/)
 * (@onetrev)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386351)
 * Excellent! That worked perfectly, I should have known I needed to access the 
   $post data.
 * Not sure I understand your question regarding the shortcode builder. I have a
   shortcode created to load the overall repeater template but this query above 
   is not an ajax repeater just a simple query. I don’t want an ajax load more inside
   another ajax load more…. if that makes sense?
 * Regardless, thanks a ton for the help. Cheers!
 *  Plugin Author [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * (@dcooney)
 * [10 years, 2 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386352)
 * > Not sure I understand your question regarding the shortcode builder.
 * Me neither, I think I misunderstand what was happening in your repeater template.
 * Glad it’s all sorted out for you.
 * Cheers,

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘WP_Query within a repeater’ is closed to new replies.

 * ![](https://ps.w.org/ajax-load-more/assets/icon-256x256.png?rev=2944639)
 * [Ajax Load More – Infinite Scroll, Load More, & Lazy Load](https://wordpress.org/plugins/ajax-load-more/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/ajax-load-more/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/ajax-load-more/)
 * [Active Topics](https://wordpress.org/support/plugin/ajax-load-more/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/ajax-load-more/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/ajax-load-more/reviews/)

## Tags

 * [ajax](https://wordpress.org/support/topic-tag/ajax/)
 * [custom](https://wordpress.org/support/topic-tag/custom/)
 * [wp_query](https://wordpress.org/support/topic-tag/wp_query/)

 * 6 replies
 * 2 participants
 * Last reply from: [Darren Cooney](https://wordpress.org/support/users/dcooney/)
 * Last activity: [10 years, 2 months ago](https://wordpress.org/support/topic/query-within-a-repeater/#post-7386352)
 * Status: resolved