I have the following code, which causes an infinite loop for some reason. Can anybody explain what's going on please?
Thanks!
<?php
$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">
<tr>
<th>Store</th>
<th>Flag Reason</th>
<th>Delete Flag</th>
</tr>
<?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;?>
</table>
<?php else: ?>
No flags found.
<?php endif; ?>
I have the following code, which causes an infinite loop for some reason. Can anybody explain what's going on please?
Thanks!
<?php
$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">
<tr>
<th>Store</th>
<th>Flag Reason</th>
<th>Delete Flag</th>
</tr>
<?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;?>
</table>
<?php else: ?>
No flags found.
<?php endif; ?>
Share
Improve this question
asked Nov 20, 2011 at 9:32
yuvalyuval
1211 silver badge2 bronze badges
4 Answers
Reset to default 3Look at this answer: get custom post type by tag
I believe you'd use $flagged_stores->the_post()
inside while
loop.
try and use 'posts_per_page'
instead of 'limit'
- http://codex.wordpress/Class_Reference/WP_Query#Parameters
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'
.
As pointed out by Michael in a comment to brownian, use while
and don't forget $your_query->the_post();
that iterates the post index. Wrap while
in if($your_query->have_posts())
to have an option for no entries.
<?php
if($flagged_stores->have_posts()):
while($flagged_stores->have_posts()):
$flagged_stores->the_post();
?>
<!-- your html -->
<?php
endwhile;
else:
?>
No flags found.
<?php
endif;
?>