I have the following code, which causes an infinite loop for some reason. Can anybody explain what's going on please?



$flagged_stores = new WP_Query( array ( 'post_type' => 'store', 'orderby' => 'post_id', 'meta_key' => 'flagged', 'limit' => 10 ) );

   if($flagged_stores->have_posts()): ?> 
        <div class="table">
            <table class="form-table">
                    <th>Flag Reason</th>
                    <th>Delete Flag</th>
                <?php while($flagged_stores->have_posts()): ?>
                    <td><?php echo the_title(); ?></td>
                    <td><?php// echo get_post_custom_values('flagged'); ?></td>
                    <td><?php// echo "Delete"; ?></td>
                <?php endwhile;?>
    <?php else: ?>
            No flags found.
    <?php endif; ?>

Look at this answer: get custom post type by tag

I believe you'd use $flagged_stores->the_post() inside while loop.

  • +1 - well spotted; it should indeed read: <?php while($flagged_stores->have_posts()): $flagged_stores->the_post(); ?> - @yuval: did the endless loop always show the same title? – Michael Nov 20 '11 at 21:50
  • 1
    @Michael yes, because you don't seek post by post without the_post() – brownian Nov 21 '11 at 7:29
  • Also don't do echo the_title() it's just the_title(), as it prints itself by default. Usually you use echo with functions starting with get_, like in echo get_the_title() – aesede Aug 28 '15 at 20:50

try and use 'posts_per_page' instead of 'limit' - http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  • Still no luck. Any other ideas? Thanks! – yuval Nov 20 '11 at 17:52

I do not know if this is what caused the infinite loop, but your WP_Query is off.

$flagged_stores = new WP_Query( array ( 'post_type' => 'store', 'orderby' => 'ID', 'meta_key' => 'flagged', 'posts_per_page' => 10 ) );

I updated 'orderby' and 'posts_per_page'.

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.