最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

post meta - Why am I getting an infinite loop with have_posts?

programmeradmin1浏览0评论

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
Add a comment  | 

4 Answers 4

Reset to default 3

Look 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; 
?>
发布评论

评论列表(0)

  1. 暂无评论